1

Im trying to make a query in asp.net models.

Im understand we can get a record by a primary key with ModelContext.Table.Find(id), and a list of all items with ModelContext.Table.ToList()...

My question is, how i can get a single item and entire list of items looking in another columns? for example name and lastname.

Something like ModelContext.Table.Find(new {name = "Stefan", lastname = "Luv"}) and ModelContext.Table.FindAll(new {"country"="usa"})

Thanks.

Stefan Luv
  • 1,179
  • 3
  • 12
  • 28

1 Answers1

1

Use Where:

ModelContext.Table.Where(item => item.lastname == "Luv" && item.name == "stefan").ToList();

This is a specific example but you can of course set many kinds of predicates in the where expression.

If you want a single unique item, you can use Single():

ModelContext.Table.Single(item => item.lastname == "Luv" && item.name == "stefan");

Note this will throw an exception in case there is more than one row that satisfies your predicate.

Ofiris
  • 6,047
  • 6
  • 35
  • 58
  • Also see this question for more details: http://stackoverflow.com/questions/2724096/linq-single-vs-first – Ofiris Oct 03 '14 at 23:22