Where Not in List<string>

雨燕双飞 提交于 2021-01-28 05:20:16

问题


I have a List<string> that contains a number of string values. Can I filter a collection when doing a query with a list instead of a single value?

Something like this:

List<string> slist = new List<string>() { "val 1", "val 2", "val 3" };
var q = (from s in ctx.Shipments
         where !s.ShipName.ToList().Contains(slist)).ToList()

回答1:


What you need is !slist.Contains(s.ShipName) like:

(from s in ctx.Shipments 
 where !slist.Contains(s.ShipName)
).ToList();

You may see this article for creating IN query with LINQ




回答2:


from sn in ctx.ShipNames
where !slist.Contains(sn.Name)
select sn.Shipment

May be you can go from the ShipName entity




回答3:


Try this one:

List<string> slist = new List<string>() {"val 1", "val 2", "val 3"};
(from s in ctx.Shipments where !slist.Contains(s.ShipName).ToList();

This way you will get all the records of the Shipments, whose name isn't included in the slist.



来源:https://stackoverflow.com/questions/22182767/where-not-in-liststring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!