Skip to main content
Mentionned the EF version i'm using
Source Link
Axel Samyn
  • 406
  • 3
  • 13

This isn't exactly the IN operator, but it might help you get the expected result and maybe a more generic approach (as it allows two collections to be compared) : INTERSECT

here's a working example

var selected = users.Where(u => new[] { "Admin", "User", "Limited" }.Intersect(new[] {u.User_Rights}).Any() ); OR var selected = users.Where(u => new[] {u.User_Rights}.Intersect(new[] { "Admin", "User", "Limited" }).Any() ); 

I guess performance should be benchmarked (against the currently accepted answer) to fully validate this solution...

EDIT :

As Gert Arnold asked for an example (EF 6) : This piece of code gives me any user whose first and/or last name matches "John" or "Doe" :

// GET: webUsers public async Task<ActionResult> Index() { var searchedNames = new[] { "John", "Doe" }; return View( await db .webUsers .Where(u => new[] { u.firstName, u.lastName }.Intersect(searchedNames).Any()) .ToListAsync() ); //return View(await db.webUsers.ToListAsync()); } 

This isn't exactly the IN operator, but it might help you get the expected result and maybe a more generic approach (as it allows two collections to be compared) : INTERSECT

here's a working example

var selected = users.Where(u => new[] { "Admin", "User", "Limited" }.Intersect(new[] {u.User_Rights}).Any() ); OR var selected = users.Where(u => new[] {u.User_Rights}.Intersect(new[] { "Admin", "User", "Limited" }).Any() ); 

I guess performance should be benchmarked (against the currently accepted answer) to fully validate this solution...

EDIT :

As Gert Arnold asked for an example : This piece of code gives me any user whose first and/or last name matches "John" or "Doe" :

// GET: webUsers public async Task<ActionResult> Index() { var searchedNames = new[] { "John", "Doe" }; return View( await db .webUsers .Where(u => new[] { u.firstName, u.lastName }.Intersect(searchedNames).Any()) .ToListAsync() ); //return View(await db.webUsers.ToListAsync()); } 

This isn't exactly the IN operator, but it might help you get the expected result and maybe a more generic approach (as it allows two collections to be compared) : INTERSECT

here's a working example

var selected = users.Where(u => new[] { "Admin", "User", "Limited" }.Intersect(new[] {u.User_Rights}).Any() ); OR var selected = users.Where(u => new[] {u.User_Rights}.Intersect(new[] { "Admin", "User", "Limited" }).Any() ); 

I guess performance should be benchmarked (against the currently accepted answer) to fully validate this solution...

EDIT :

As Gert Arnold asked for an example (EF 6) : This piece of code gives me any user whose first and/or last name matches "John" or "Doe" :

// GET: webUsers public async Task<ActionResult> Index() { var searchedNames = new[] { "John", "Doe" }; return View( await db .webUsers .Where(u => new[] { u.firstName, u.lastName }.Intersect(searchedNames).Any()) .ToListAsync() ); //return View(await db.webUsers.ToListAsync()); } 
clarification
Source Link
Axel Samyn
  • 406
  • 3
  • 13

This isn't exactly the IN operator, but it might help you get the expected result and maybe a more generic approach (as it allows two collections to be compared) : INTERSECT

here's a working example

var selected = users.Where(u => new[] { "Admin", "User", "Limited" }.Intersect(new[] {u.User_Rights}).Any() ); OR var selected = users.Where(u => new[] {u.User_Rights}.Intersect(new[] { "Admin", "User", "Limited" }).Any() ); 

I guess performance should be benchmarked (against the currently accepted answer) to fully validate this solution...

EDIT :

As Gert Arnold asked for an example : This piece of code gives me any user whose first and/or last name matches "John" or "Doe" :

// GET: webUsers public async Task<ActionResult> Index() { var searchedNames = new[] { "John", "Doe" }; return View( await db .webUsers .Where(u => new[] { u.firstName, u.lastName }.Intersect(searchedNames).Any()) .ToListAsync() ); //return View(await db.webUsers.ToListAsync()); } 

This isn't exactly the IN operator, but it might help you get the expected result and maybe a more generic approach (as it allows two collections to be compared) : INTERSECT

here's a working example

var selected = users.Where(u => new[] { "Admin", "User", "Limited" }.Intersect(new[] {u.User_Rights}).Any() ); OR var selected = users.Where(u => new[] {u.User_Rights}.Intersect(new[] { "Admin", "User", "Limited" }).Any() ); 

I guess performance should be benchmarked (against the currently accepted answer) to fully validate this solution...

This isn't exactly the IN operator, but it might help you get the expected result and maybe a more generic approach (as it allows two collections to be compared) : INTERSECT

here's a working example

var selected = users.Where(u => new[] { "Admin", "User", "Limited" }.Intersect(new[] {u.User_Rights}).Any() ); OR var selected = users.Where(u => new[] {u.User_Rights}.Intersect(new[] { "Admin", "User", "Limited" }).Any() ); 

I guess performance should be benchmarked (against the currently accepted answer) to fully validate this solution...

EDIT :

As Gert Arnold asked for an example : This piece of code gives me any user whose first and/or last name matches "John" or "Doe" :

// GET: webUsers public async Task<ActionResult> Index() { var searchedNames = new[] { "John", "Doe" }; return View( await db .webUsers .Where(u => new[] { u.firstName, u.lastName }.Intersect(searchedNames).Any()) .ToListAsync() ); //return View(await db.webUsers.ToListAsync()); } 
Forgot to append the .Any() call to the intersect result
Source Link
Axel Samyn
  • 406
  • 3
  • 13

This isn't exactly the IN operator, but it might help you get the expected result and maybe a more generic approach (as it allows two collections to be compared) : INTERSECT

here's a working example

var selected = users.Where(u => new[] { "Admin", "User", "Limited" }.Intersect(new[] {u.User_Rights}).Any() ); OR var selected = users.Where(u => new[] {u.User_Rights}.Intersect(new[] { "Admin", "User", "Limited" }).Any() ); 

I guess performance should be benchmarked (against the currently accepted answer) to fully validatedvalidate this solution...

This isn't exactly the IN operator, but it might help you get the expected result and maybe a more generic approach (as it allows two collections to be compared) : INTERSECT

here's a working example

var selected = users.Where(u => new[] { "Admin", "User", "Limited" }.Intersect(new[] {u.User_Rights}) ); OR var selected = users.Where(u => new[] {u.User_Rights}.Intersect(new[] { "Admin", "User", "Limited" }) ); 

I guess performance should be benchmarked (against the currently accepted answer) to fully validated this solution...

This isn't exactly the IN operator, but it might help you get the expected result and maybe a more generic approach (as it allows two collections to be compared) : INTERSECT

here's a working example

var selected = users.Where(u => new[] { "Admin", "User", "Limited" }.Intersect(new[] {u.User_Rights}).Any() ); OR var selected = users.Where(u => new[] {u.User_Rights}.Intersect(new[] { "Admin", "User", "Limited" }).Any() ); 

I guess performance should be benchmarked (against the currently accepted answer) to fully validate this solution...

Stack Overflow is like an encyclopedia, so we prefer to omit these types of phrases. It is assumed that everyone here is trying to be helpful.
Source Link
Dharman
  • 33.9k
  • 27
  • 106
  • 157
Loading
Source Link
Axel Samyn
  • 406
  • 3
  • 13
Loading