【问题标题】:EF: where..in clause with multiple columns [duplicate]EF:具有多列的 where..in 子句[重复]
【发布时间】:2018-07-19 00:13:54
【问题描述】:

只是想抓住 EF。当我们使用 sql 时,我们经常在 in 子句中写入多个值

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')

【问题讨论】:

标签: c# entity-framework


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2011-10-18
    • 2014-02-14
    • 2019-06-24
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多