Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF:where..in具有多列的子句_C#_Entity Framework - Fatal编程技术网

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

C# EF:where..in具有多列的子句,c#,entity-framework,C#,Entity Framework,只是想抓住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.Wh

只是想抓住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;
告诉我如何在不使用多个contains关键字的情况下将下面的sql查询转换为EF

Select * from customer
Where countryCode in ('gb','us','fr')

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

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

var locations = Location.Where(loc => countryCodes.Contains(loc.Country));
var countryCodes=新列表{“gb”、“us”、“fr”}
变量位置=位置。其中(loc=>countryCodes.Contains(loc.Country));

请不要编辑您问题的答案。相反,在重复的帖子上贴一个答案。