如何在 LINQ 中对单个连接中的多个字段进行连接 [英] How to do joins in LINQ on multiple fields in single join

查看:30
本文介绍了如何在 LINQ 中对单个连接中的多个字段进行连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做一个 LINQ2DataSet 查询,该查询对多个字段进行连接(如

I need to do a LINQ2DataSet query that does a join on more than one field (as

var result = from x in entity
join y in entity2 
       on x.field1 = y.field1 
and 
          x.field2 = y.field2

我还没有找到合适的解决方案(我可以在 where 子句中添加额外的约束,但这远不是合适的解决方案,或者使用 this 解决方案,但假设是等值联接).

I have yet found a suitable solution (I can add the extra constraints to a where clause, but this is far from a suitable solution, or use this solution, but that assumes an equijoin).

在 LINQ 中是否可以在单个连接中连接多个字段?

Is it possible in LINQ to join on multiple fields in a single join?

编辑

var result = from x in entity
             join y in entity2
             on new { x.field1, x.field2 } equals new { y.field1, y.field2 }

是我在上面提到的假设相等的解决方案.

is the solution I referenced as assuming an equijoin above.

进一步编辑

为了回答关于我的原始示例是 equijoin 的批评,我承认,我当前的要求是 equijoin,并且我已经采用了我上面引用的解决方案.

To answer criticism that my original example was an equijoin, I do acknowledge that, My current requirement is for an equijoin and I have already employed the solution I referenced above.

然而,我试图了解我有/应该在 LINQ 中采用哪些可能性和最佳实践.我很快需要对表 ID 进行日期范围查询连接,并且只是在解决这个问题,看来我必须在 where 子句中添加日期范围.

I am, however, trying to understand what possibilities and best practices I have / should employ with LINQ. I am going to need to do a Date range query join with a table ID soon, and was just pre-empting that issue, It looks like I shall have to add the date range in the where clause.

一如既往地感谢您提出的所有建议和意见

Thanks, as always, for all suggestions and comments given

推荐答案

匿名类型的解决方案应该可以正常工作.LINQ 可以 只表示 equijoins(无论如何都带有 join 子句),而且确实这就是您所说的基于原始查询无论如何要表达的内容.

The solution with the anonymous type should work fine. LINQ can only represent equijoins (with join clauses, anyway), and indeed that's what you've said you want to express anyway based on your original query.

如果您因为某些特定原因不喜欢匿名类型的版本,您应该说明原因.

If you don't like the version with the anonymous type for some specific reason, you should explain that reason.

如果您想做一些与您最初要求不同的事情,请举例说明您真正想做的事情.

If you want to do something other than what you originally asked for, please give an example of what you really want to do.

回应问题中的是的,要进行日期范围"连接,您需要改用 where 子句.它们在语义上实际上是等效的,因此这只是可用优化的问题.Equijoins 通过基于内部序列创建查找来提供简单的优化(在 LINQ to Objects 中,包括 LINQ to DataSets) - 将其视为从键到匹配该键的条目序列的哈希表.

Responding to the edit in the question: yes, to do a "date range" join, you need to use a where clause instead. They're semantically equivalent really, so it's just a matter of the optimisations available. Equijoins provide simple optimisation (in LINQ to Objects, which includes LINQ to DataSets) by creating a lookup based on the inner sequence - think of it as a hashtable from key to a sequence of entries matching that key.

在日期范围内这样做有点困难.但是,根据日期范围连接"的确切含义,您可以做一些类似的 - 如果您计划创建日期的带"(例如每年一个),例如发生在同一年(但不在同一日期)的两个条目应该匹配,然后您只需使用该乐队作为键就可以做到这一点.如果它更复杂,例如连接的一侧提供一个范围,连接的另一侧提供一个日期,如果它落在该范围内,则匹配,最好使用 where 子句(在第二个 from 子句)IMO.您可以通过订购一侧或另一侧来更有效地找到匹配项来做一些特别时髦的魔术,但这将是很多工作 - 我只会在检查性能是否有问题后才做这种事情.

Doing that with date ranges is somewhat harder. However, depending on exactly what you mean by a "date range join" you may be able to do something similar - if you're planning on creating "bands" of dates (e.g. one per year) such that two entries which occur in the same year (but not on the same date) should match, then you can do it just by using that band as the key. If it's more complicated, e.g. one side of the join provides a range, and the other side of the join provides a single date, matching if it falls within that range, that would be better handled with a where clause (after a second from clause) IMO. You could do some particularly funky magic by ordering one side or the other to find matches more efficiently, but that would be a lot of work - I'd only do that kind of thing after checking whether performance is an issue.

这篇关于如何在 LINQ 中对单个连接中的多个字段进行连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆