EF:where..in具有多列的子句

时间:2018-02-08 12:14:31

标签: c# entity-framework

试图抓住EF。当我们使用sql时,我们经常在子句

中写入多个值
Select * from customer
Where countryCode in ('gb','us','fr')

我正在搜索如何使用EF和LINQ编写相同的查询。我找到了这些代码。

var countries= new[] {
    new {Country=…, City=…, Address=…},
    …
}

approach 1
------------
var result = locations.Where(l => keys.Any(k => 
                    k.Country == l.Country && 
                    k.City == l.City && 
                    k.Address == l.Address));

approach 2
------------
var result = from loc in Location
             where keys.Contains(new {
                 Country=loc.Country, 
                 City=loc.City, 
                 Address=loc.Address
             }
             select loc;

告诉我如何在不使用多个包含关键字

的情况下将sql查询转换为EF
Select * from customer
Where countryCode in ('gb','us','fr')

1 个答案:

答案 0 :(得分:2)

如果我理解正确,你可以这样做

var countryCodes = new List<string> {"gb","us","fr"}

var locations = Location.Where(loc => countryCodes.Contains(loc.Country));