添加将linq联接到对象c#的条件

本文关键字:对象 条件 linq 添加 | 更新日期: 2023-09-27 18:22:38

我想根据另一个属性向下面的查询添加一个条件

例如"and a.City=b.City"。我该怎么做呢。

当前查询

var result = firstCollection.Join(secondCollection, 
                                  a => a.CustomerId, 
                                  b => b.CustomerId,  //TO ADD "and a.City=b.City" 
                                  GetDifferences)
                            .SelectMany(x => x)
                            .Where(x => x != null).ToList();

在sql中,我会做:

Select * from firstCollection a 
INNER JOIN secondCollection B on a.CustomerId=b.CustomerId and a.city=b.city

非常感谢您的建议

添加将linq联接到对象c#的条件

使用匿名类型:

var result = firstCollection.Join(secondCollection, 
                                  a => new { a.CustomerId, a.City }
                                  b => new { b.CustomerId, b.City },
                                  GetDifferences)
                            .SelectMany(x => x)
                            .Where(x => x != null).ToList();