我需要做一个LINQ2DataSet查询,对多个字段(如

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

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

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

EDIT

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

是我在上面假设等联时引用的解决方案。

进一步的编辑

为了回答批评,我原来的例子是一个等量连接,我承认,我目前的要求是一个等量连接,我已经采用了我上面提到的解决方案。

然而,我正在尝试了解我已经/应该使用LINQ的可能性和最佳实践。我将需要做一个日期范围查询连接与表ID很快,只是先发制人的问题,看起来我必须在where子句中添加日期范围。

一如既往,感谢所有的建议和意见


当前回答

使用匿名类型的解决方案应该可以正常工作。LINQ只能表示等连接(使用连接子句),实际上,这正是您根据原始查询想要表达的内容。

如果您因为某些特定的原因而不喜欢匿名类型的版本,那么您应该解释这个原因。

如果你想做的事情与你最初要求的不同,请举例说明你真正想做的事情。

编辑:回答问题中的编辑:是的,要做一个“日期范围”连接,你需要使用一个where子句。它们在语义上是等效的,所以这只是一个可用的优化问题。等连接提供了简单的优化(在LINQ到对象中,包括LINQ到数据集),通过创建基于内部序列的查找-可以将其视为从键到匹配该键的条目序列的哈希表。

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.

其他回答

我用元组来做,这是一个有两列的例子:

 var list= list1.Join(list2,
                       e1 => (e1.val1,e1.val2),
                       e2 => (e2.val1,e2.val2),
                       (e1, e2) => e1).ToList();

使用匿名类型的解决方案应该可以正常工作。LINQ只能表示等连接(使用连接子句),实际上,这正是您根据原始查询想要表达的内容。

如果您因为某些特定的原因而不喜欢匿名类型的版本,那么您应该解释这个原因。

如果你想做的事情与你最初要求的不同,请举例说明你真正想做的事情。

编辑:回答问题中的编辑:是的,要做一个“日期范围”连接,你需要使用一个where子句。它们在语义上是等效的,所以这只是一个可用的优化问题。等连接提供了简单的优化(在LINQ到对象中,包括LINQ到数据集),通过创建基于内部序列的查找-可以将其视为从键到匹配该键的条目序列的哈希表。

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.

您可以这样做(如下所示)

var query = from p in context.T1

        join q in context.T2

        on

        new { p.Col1, p.Col2 }

        equals

         new { q.Col1, q.Col2 }

        select new {p...., q......};
var result = from x in entity
   join y in entity2 on new { x.field1, x.field2 } equals new { y.field1, y.field2 }
from d in db.CourseDispatches
                             join du in db.DispatchUsers on d.id equals du.dispatch_id
                             join u in db.Users on du.user_id equals u.id
                             join fr in db.Forumreports on (d.course_id + '_' + du.user_id)  equals  (fr.course_id + '_'+ fr.uid)

这对我很有用