问题
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