C# EF:where..in具有多列的子句
只是想抓住EF。当我们使用sql时,我们经常在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
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));
请不要编辑您问题的答案。相反,在重复的帖子上贴一个答案。