chobo2
chobo2

Reputation: 85715

NHibernate Linq where clause: value in collection

I am wondering can I do a where clause that takes in a collection?

List<string> myStrings = new List<strings> {"1", "2"};

session.Query<Table>().Where(x => x.Id == myStrings).ToList();

I basically want to get all rows from my db table that match everything in that query.

session.Query<Table>().Where(x => x.Id == myStrings[0]).ToList();
session.Query<Table>().Where(x => x.Id == myStrings[1]).ToList();
session.Query<Table>().Where(x => x.Id == myStrings[N]).ToList();

So thats what I would have to do right now. I would probably through that in a for loop but that is alot of queryies and I rather just do one query.

Or do I have to use the nhibernate create query syntax

var query = "Select * From Where In (:Id)";
session.CreateQuery(query)SetParameter("Id",myStrings) // not sure if I have to something like .ExecuteUpdate(); but just for select instead

Upvotes: 2

Views: 2978

Answers (3)

psousa
psousa

Reputation: 6726

session.Query<Table>().Where(x => myStrings.Contains(s => x.Id));

Upvotes: 4

Femaref
Femaref

Reputation: 61427

session.Query<Table>().Where(x => myString.All(s => x.Id == s));

Upvotes: 1

frabiacca
frabiacca

Reputation: 1452

you should use Any or All extension method on your collection

Upvotes: 0

Related Questions