How to do joins in LINQ on multiple fields in single join

前端 未结 13 2053
小鲜肉
小鲜肉 2020-11-22 17:02

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          


        
13条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 18:07

    I think a more readable and flexible option is to use Where function:

    var result = from x in entity1
                 from y in entity2
                     .Where(y => y.field1 == x.field1 && y.field2 == x.field2)
    

    This also allows to easily change from inner join to left join by appending .DefaultIfEmpty().

提交回复
热议问题