Linq 连接参数化的不同键

2023-12-31

我正在尝试基于动态键 LINQ 两个表。用户可以通过组合框更改密钥。键可能是钱、字符串、双精度数、整数等。目前我得到的数据很好,但没有过滤掉双精度数。我可以在 VB 中过滤双精度值,但速度很慢。我想立即在 LINQ 查询中执行此操作。

这是数据:

第一张表:

 -------------------------------------------------------------
| AppleIndex  | AppleCost  | AppleColor  | AppleDescription   |
 ------------------------------------------------------------
|     1       |     3      | Red         | This is an apple   |
|     2       |     5      | Green       | This is an apple   |
|     3       |     4      | Pink        | This is an apple   |
|     4       |     2      | Yellow      | This is an apple   |
|     5       |     2      | Orange      | This is an apple   |
|     1       |     3      | Red         | This is a duplicate|
|     2       |     5      | Green       | This is a duplicate|
|     3       |     4      | Pink        | This is a duplicate|
|     4       |     2      | Yellow      | This is a duplicate|
|     5       |     2      | Orange      | This is a duplicate|
 -------------------------------------------------------------

第二个表:

 ------------------------------------------------------------
| OrangeIndex | OrangeCost | OrangeColor | OrangeDescription |
 ------------------------------------------------------------
|     1       |     1      | Orange      | This is an Orange |
|     2       |     3      | Orange      |                   |
|     3       |     2      | Orange      | This is an Orange |
|     4       |     3      | Orange      |                   |
|     5       |     2      | Orange      | This is an Orange |
 ------------------------------------------------------------

目前,我正在使用以下代码来获取太多数据:

Dim Matches = From mRows In LinqMasterTable Join sRows In LinqSecondTable _
              On mRows(ThePrimaryKey) Equals sRows(TheForignKey) _
              Order By mRows(ThePrimaryKey) _
              Select mRows, sRows Distinct

Outcome:

 -------------------------------------------------------------------------
| 1  | 3 | Red    | This is an apple     | 1 | Orange | This is an Orange |
| 1  | 3 | Red    | This is an duplicate | 1 | Orange | This is an Orange |
| 2  | 5 | Green  | This is an apple     | 3 | Orange |                   |
| 2  | 5 | Green  | This is an duplicate | 3 | Orange |                   |
| 3  | 4 | Pink   | This is an apple     | 2 | Orange | This is an Orange |
| 3  | 4 | Pink   | This is an duplicate | 2 | Orange | This is an Orange |
| 4  | 2 | Yellow | This is an apple     | 3 | Orange |                   |
| 4  | 2 | Yellow | This is an duplicate | 3 | Orange |                   |
| 5  | 2 | Orange | This is an apple     | 2 | Orange | This is an Orange |
| 5  | 2 | Orange | This is an duplicate | 2 | Orange | This is an Orange |
 -------------------------------------------------------------------------

期望的结果:

 ------------------------------------------------------------------------
| 1 | 3 | Red    | This is an apple | 1 | 1 | Orange | This is an Orange |
| 2 | 5 | Green  | This is an apple | 2 | 3 | Orange |                   |
| 3 | 4 | Pink   | This is an apple | 3 | 2 | Orange | This is an Orange |
| 4 | 2 | Yellow | This is an apple | 4 | 3 | Orange |                   |
| 5 | 2 | Orange | This is an apple | 5 | 2 | Orange | This is an Orange |
 ------------------------------------------------------------------------

我已经尝试过以下方法:

'Get the original Column Names into an Array List
'MasterTableColumns = GetColumns(qMasterDS, TheMasterTable) '(external code)

'Plug the Existing DataSet into a DataView:
Dim View As DataView = New DataView(qMasterTable)

'Sort by the Primary Key:
View.Sort = ThePrimaryKey

'Build a new table listing only one column:
Dim newListTable As DataTable = _
View.ToTable("UniqueData", True, ThePrimaryKey)

这将返回一个唯一列表,但没有关联数据:

 -------------
| AppleIndex  |
 -------------
|     1       | 
|     2       | 
|     3       |
|     4       |
|     5       |
 -------------

所以我尝试了这个:

'Build a new table with ALL the columns:
Dim newFullTable As DataTable = _
View.ToTable("UniqueData", True, _
     MasterTableColumns(0), _
     MasterTableColumns(1), _
     MasterTableColumns(2), _
     MasterTableColumns(3))

不幸的是,它产生以下结果......有重复项:

 -------------------------------------------------------------
| AppleIndex  | AppleCost  | AppleColor  | AppleDescription   |
 ------------------------------------------------------------
|     1       |     3      | Red         | This is an apple   |
|     2       |     5      | Green       | This is an apple   |
|     3       |     4      | Pink        | This is an apple   |
|     4       |     2      | Yellow      | This is an apple   |
|     5       |     2      | Orange      | This is an apple   |
|     1       |     3      | Red         | This is a duplicate|
|     2       |     5      | Green       | This is a duplicate|
|     3       |     4      | Pink        | This is a duplicate|
|     4       |     2      | Yellow      | This is a duplicate|
|     5       |     2      | Orange      | This is a duplicate|
 -------------------------------------------------------------

有任何想法吗?

~~~~~~~~~~~~更新:~~~~~~~~~~~~

Jeff M 建议使用以下代码。 (谢谢杰夫)但是,它给了我一个错误。有谁知道在 VB 中实现此功能的语法吗?我已经搞砸了一点,但似乎无法做到正确。

Dim matches = _
    From mRows In (From row In LinqMasterTable _
        Group row By row(ThePrimaryKey) Into g() _
        Select g.First()) _
    Join sRows In LinqSecondTable _
    On mRows(ThePrimaryKey) Equals sRows(TheForignKey) _
    Order By mRows(ThePrimaryKey) _
    Select mRows, sRows

“row(ThePrimaryKey)”第三行出错:

“范围变量名称只能从不带参数的简单名称或限定名称推断出来。”


嗯,基本问题不是 LINQ。事实上,您的第一个表包含“重复项”,它们并不是真正的重复项,因为在您的示例中,每一行都是独特的。

因此,我们向您提出的问题是“我们如何识别原始表中的重复项?”。一旦回答了这个问题,剩下的事情就变得微不足道了。

例如(在 C# 中,因为我不确定 VB 语法)

var Matches = from mRows in LinqMasterTable
                             .Where(r=>r.AppleDescription=="This is an Apple")
              join sRows in LinqSecondTable 
                   on mRows(ThePrimaryKey) equals sRows(TheForignKey)  
              orderby mRows(ThePrimaryKey) 
              select new { mRows, sRows};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Linq 连接参数化的不同键 的相关文章

随机推荐

  • 在中断例程中使用 C++ 对象(和易失性对象)的正确方法是什么?

    我目前正在使用 Atmel AVR 微控制器 gcc 但希望答案适用于一般微控制器世界 即通常是单线程但带有中断 我知道如何使用volatile在 C 代码中访问可在 ISR 中修改的变量时 例如 uint8 t g pushIndex 0
  • SDWebImage UITableView 单元格图像不持久

    我正在使用 SDWebImage 库来从 Web 服务异步下载和缓存图像 以下是我用来下载图像的方法 void downloadThumbnails NSURL finalUrl SDWebImageManager manager SDWe
  • 当用户使用 django all-auth 注册时如何创建新的模型对象?

    当用户注册时 我想创建一个profile像这样的对象 profile Profile objects create username username points 0 age 0 Django all auth 的注册视图如下所示 cla
  • Linux 中文件更改时重新启动进程

    是否有一个简单的解决方案 使用常见的 shell utils 通过大多数发行版提供的 utils 或一些简单的 python 脚本 来在某些文件更改时重新启动进程 简单地调用某物就好了watch cmd the process arg de
  • 如果我必须为 Oracle .net Entity Framework 定义 app.config 中的每个字段,那么 ORM 的意义何在?

    据我所知 实体框架的重点是简化和统一数据访问模型 我下载了最新的 Oracle net 工具 ODAC 11 2 第 4 版 该工具声称支持 EF4 并且我正在尝试导入一些 Oracle 存储过程 通过函数导入 该过程将一个引用游标作为输出
  • R中的树列表到反转下三角矩阵

    我该如何进行转换 m list 1 2 3 4 6 7 10 to 1 2 3 4 1 0 0 0 10 2 0 0 6 9 3 0 3 5 8 4 1 2 4 7 一个想法或一些指导表示赞赏 感谢您的耐心等待 以防问题太天真或需要其他信息
  • CentOS 上的 Apache Ant 1.8

    我正在尝试让 apache ant 1 8 在 CentOS 下工作 首先 我遇到了这个错误 Error Could not find or load main class org apache tools ant launch Launc
  • Nextjs- api 路由,如何从标头中删除 cookie?

    我有两个 API 路由 我想在其中设置两个 cookie api login js并将它们删除 api logout js 这是我的登录 API import serialize from cookie export default asy
  • Php-计算出现在是什么学年

    我正在尝试编写一个适合当前学年的脚本 学年于每年8月1日开始 我如何根据当前日期计算出我们所处的学年 IE 2012 年 7 月 31 日 20120631 将计算为 2011 2012 2012 年 8 月 13 日 20120801 将
  • 将 Spring Security 升级到 3.2.0.RELEASE 不再在 Spring taglib 中提供 CSRF 令牌

    我的项目使用 Spring Security 3 2 0 RC2 我的 JSP 使用 Spring taglib 的 form form 标记自动将 CSRF 令牌插入到我的表单中 升级到 Spring Security 3 2 0 REL
  • VLOOKUP 未在数组中找到值

    I used VLOOKUP函数在数组中查找值 但某些值给出了 N A 答案 尽管在数组中可用 为了对数字进行四舍五入 我使用了CEILING功能 但有趣的一点是在某些值中 它不起作用 我检查了值的类型是否为数字 另外 我用过ROUNDUP
  • Angular2 动画[隐藏]元素

    我有一个侧边栏 我使用如下布尔值显示 隐藏 hidden toggleSidebar 我一直在尝试找到向此元素添加过渡的正确方法 但到目前为止 使用此方法仅部分成功 class show toggleSidebar 不过 将 css 样式应
  • 从 Python shell 使用 IPython,如“code.interact()”

    是否可以使用现有 Python shell 中的 IPython shell 作为 shell inside a shell 类似于内置的code interact In Python 0 11API 已经过彻底修改 shell 更容易调用
  • 内置容器的自定义比较

    在我的代码中 对各种容器 列表 字典等 的相等性进行了大量比较 容器的键和值的类型为 float bool int 和 str 内置的 和 工作得很好 我刚刚了解到 必须使用自定义比较函数来比较容器值中使用的浮点数 我已经编写了该函数 我们
  • Google OR-Tools(使用 SCIP 求解器)- 如何访问求解器找到的中间解?

    我是 Google OR Tools 的新手 我使用 Python 实现了一个以 SCIP 作为求解器的 MIP 模型 目标函数用于最小化 求解器 最小化 C 并且我正在通过访问最终解决方案求解器 Objective Value 但是 我还
  • Eclipse Mars 对 Java 的支持 - 构建路径设置

    我尝试安装并使用Java 9 对 Eclipse Mars 的支持 http www eclipse org community eclipse newsletter 2015 june article4 php 然而 当尝试编译一个简单的
  • MKLocalSearch 只产生美国结果,如何扩展搜索?

    我正在使用 MKLocalSearch 来允许用户搜索他们的城市 但是 当我尝试该代码时 我只收到美国的结果 我还收到了很多结果 包括商店等 我添加了过滤器func completerDidUpdateResults completer M
  • 清除淘汰验证错误

    我有一个使用 Knockout js 并使用 Knockout Validation 的页面设置 在页面加载期间 我在选择框中放置了另一个插件 该插件会触发更改 从而触发验证 我需要能够使用 JS 清除该错误 以便我可以从外观新颖的 UI
  • 为什么计算斐波那契数列的复杂度是 2^n 而不是 n^2?

    我试图使用递归树找到斐波那契数列的复杂性并得出结论height of tree O n 最坏的情况下 cost of each level cn hence complexity n n n 2 怎么会这样O 2 n 朴素递归斐波那契的复杂
  • Linq 连接参数化的不同键

    我正在尝试基于动态键 LINQ 两个表 用户可以通过组合框更改密钥 键可能是钱 字符串 双精度数 整数等 目前我得到的数据很好 但没有过滤掉双精度数 我可以在 VB 中过滤双精度值 但速度很慢 我想立即在 LINQ 查询中执行此操作 这是数