Question

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()
Était-ce utile?

La solution

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

Autres conseils

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

May be you can go from the ShipName entity

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top