zsharp
zsharp

Reputation: 13756

Ways to match list items with linq sql objects

I have a List and I want to see if any of these strings are in any of 3 fields of a single sql record using linq to sql.

   ListA<strings>;
   var found=db.People.Where(p=>p.field1 field 2 or field 3 is in ListA). Select this person

Upvotes: 0

Views: 301

Answers (2)

JotaBe
JotaBe

Reputation: 39045

Use this code:

ListA<strings>;
var found=db.People.Where(p=>
  ListA.Contains(p.field1) || ListaA.Contains(p.field2) 
  || ListaA.Contains(p.field3));

Please, be aware that this will take all the records from the DB and test the condition on the application side.

EDIT: this is just the same code of the other answer, but includes a note on how it works, so I don't delete it. (Changed because of @Adrian lftode comment)

Upvotes: 1

BlueMonkMN
BlueMonkMN

Reputation: 25601

var found = db.People.Where(p=>ListA.Contains(p.field1) || ListA.Contains(p.field2) || ListA.Contains(p.field3));

Upvotes: 2

Related Questions