Q:

Return one result from LINQ Query

if you have a select LINQ query that should only return one result, do you have to have a foreach loop to get the result?
Or is there a better way?

A:

// Will return a default value if no object is found in the DB
db.Table.SingleOrDefault(x => x.something == someParameter);

or
// Will throw an exception if records are not found
db.Table.Single(x => x.something == someParameter);

Thanks to Mehrdad for the comment...both lines have been updated.
If it's possible that your query could result in more than one record being returned, then (as mentioned in comments) Single() and SingleOrDefault() are the wrong methods to call. You would keep the same syntax, but call First() and FirstOrDefault() respectively.

A:

var myLinqObj = db.MyObjects.Take(1).SingleOrDefault();

A:

You can use either First or Single.
First returns the first row, whether there are multiple rows or just the one.
Single expects only one row to be returned, and throws an exception if there are multiple rows.
Single is therefore potentially a better choice if you expect to only have one row, so that you'll see the problem immediately and can troubleshoot it.

