scripter78
scripter78

Reputation: 1177

LINQ String Array Not IN

using LINQ how do I get this query to return all states except for the ones in the string array?

string[] states = { "FL", "CA", "IN", "AL", "MI" };
var headers = (from h in db.Headers
               where h.State != states.Any()
               select new
               {
                   description = h.Description,
                   state = h.State
               });

Upvotes: 0

Views: 295

Answers (2)

Selman Genç
Selman Genç

Reputation: 101742

Probably you should use Contains because it will be translated as NOT IN into SQL however this is how you can do it with Any, it should be translated as NOT EXISTS:

var headers = (from h in db.Headers
               where !states.Any(x => x == h.State)
               select new
                   {
                       description = h.Description,
                       state = h.State
                   });

Upvotes: 1

Christos
Christos

Reputation: 53958

You could try this:

 var headers = (from h in db.Headers
                where !states.Contains(h.State)
                select new
                {
                    description = h.Description,
                    state = h.State
                });

Upvotes: 5

Related Questions