I would like to get a LINQ-to-SQL query that returns only one item, not a collection of them?
For example, I have a list of products with a particular name. There are no products with duplicate names in the database, so I want to be able to query and return just that instance of that product.
Products product = from p in _productContext.Products where p.name.Equals('BrownShoes') select p;
How do I do something like that?
Use
Single:or
There’s no query expression syntax for Single, so you have to call it as a normal extension method. At that point your query is simpler written entirely with dot notation, hence the form above. You could write it as:
But that’s got a lot more fluff. If there isn’t exactly a single element in the result, an exception will be thrown.