NOT IN for LINQ?

I am creating a LINQ query, and I want to have a NOT IN SQL-style clause to ensure that my result does not have one of the values ​​from a comma-separated list.

I could not find a NOT IN clause for LINQ. Please suggest a solution.

+4
source share
3 answers

You need to make a! Contained in the collection of objects that you want to exclude.

var excluded = new[] { 3, 7, 19, 41 }; var v = from i in Enumerable.Range(0, 100) where !excluded.Contains(i) select i; 
+3
source

You will need the .Except () operator.

 var results = list1.Except(list2); 

http://blogs.msdn.com/b/charlie/archive/2008/07/12/the-linq-set-operators.aspx http://www.hookedonlinq.com/ExceptOperator.ashx

Note. You will need to implement iEqualityComparor to use the Except method with complex types.

+2
source

Something like that...

 string items = "1,2,3,4"; var subList = items.Split(','); var result = list.Where(item=>!subList.Contains(item.SomeStringField)); 
+1
source

Source: https://habr.com/ru/post/1346241/


All Articles