Selecting a single LINQ2SQL Entity
on 19/10/2008 - like it?
Linq2SQL is fast becoming one of my favorite frameworks for throwing quick sites together. I used to use a lot of object relational mappers but this is free and built in so .. yay :)
I’ve noticed recently a lack of understanding in trying to return a single entity from a collection. Many times when you fetch data you know there will only be 1 row but you get a collection or have to request First from an IQueryable type.
Lets say you have a Customers table. You want to fetch 1 row of type Customer.
Customer myCustomer = (from c in dataContext.Customers where c.CustomerId = 1 select c).Single<Customer> ();
myCustomer will now be of type Customer - nice and clean :)
A good all around LINQ book, not just L2SQL but Link to everything.![]()
Tags: linq2sql examples
