Best way for retrieving single record results in LINQ to SQL
linq, linq-to-sql
Solution
Try something like this:
var user = (from u in dc.Users
where u.UserName == usn
select u).FirstOrDefault();
The FirstOrDefault method returns the first element of a sequence that satisfies a specified condition or a default value if no such element is found.
Problem
If I query a table with a condition on the key field as in: ``` var user = from u in dc.Users where u.UserName == usn select u; ``` I know that I will either get zero results or one result. Should I still go ahead and retrieve the results using a for-each or is there another preferred way to handle this kind of situation.