-3

Possible Duplicate:
How would you do a “not in” query with Linq?

I have a question about a LINQ query, I have a List<int> foo; and now I need to check something like:

var output = select a from db.User where a.id not in foo select a;

how can i realize this: a.id not in foo ?

Community
  • 1
  • 1
gurehbgui
  • 13,218
  • 29
  • 100
  • 167

3 Answers3

3

If foo is a list then where !foo.Contains(a.id).

Jon
  • 413,451
  • 75
  • 717
  • 787
2

Use the contains method on the list

var output = from a in db.User where !foo.Contains(a.id) select a;
Lukazoid
  • 18,199
  • 3
  • 62
  • 83
2

My Blog about this : SQL to LINQ ( Case 7 - Filter data by using IN and NOT IN clause)

You use, where <list>.Contains( <item> )

var foo = {1, 2, 3};
var users = from p in db.users
                 where !foo.Contains(p.id)
                 select p;

Image Representation of this

enter image description here

Pranay Rana
  • 170,430
  • 35
  • 234
  • 261