如何做好加入在LINQ在单个连接多个字段 [英] How to do joins in LINQ on multiple fields in single join

查看:99
本文介绍了如何做好加入在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子句,但这是远远没有合适的解决方案,或者使用<一个href=\"http://stackoverflow.com/questions/345427/linq-to-sql-join-multiple-columns-from-the-same-table\">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.

此外修改

要回答的批评,我的原来的例子是一个等值连接,我承认,我现在的要求是一个等值连接,我已经使用我上面提到的解决方案。

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很快加入,并且只是pre-先发制人这一问题,它看起来像我将不得不添加日期范围在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的可以的只重present等值连接(用联接子句,反正),实际上这就是你说你要前preSS反正根据您的原始查询什么。

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子句来代替。他们是在语义上等效真的,所以它只是提供了最佳化的问题。等值连接通过创建基于内部序列的查找提供简单优化(在LINQ to对象,其中包括LINQ到数据集) - 认为它是从钥匙匹配该键输入序列中的哈希表

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.

这样做,与日期范围是有点困难。但是,取决于正是你一个意思是日期范围加盟你可以做一些事情的类似的 - 如果你打算创建日期带(例如每年一次)等这其中发生在同一年的两个条目(但不是在同一日期)应该匹配,那么你可以通过使用带为重点做到这一点。如果是更复杂,例如的加入一个侧面提供了一个范围,联接的另一侧提供一个日期,匹配如果它属于该范围内,将有更好处理的其中,子句(后第二个子句)国际海事组织。你可以通过订购一方或其他更有效地找到匹配做一些特别时髦的魔法,但是这将是一个大量的工作 - 我只会做那种事检查性能是否一个问题后,

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天全站免登陆