-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
  • 14,236
  • 32
  • 106
  • 178

3 Answers3

3

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

Jon
  • 428,835
  • 81
  • 738
  • 806
2

Use the contains method on the list

var output = from a in db.User where !foo.Contains(a.id) select a;
Lukazoid
  • 19,016
  • 3
  • 62
  • 85
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
  • 175,020
  • 35
  • 237
  • 263