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.
3 You can replace . Where(predicate).Single() with . Single(predicate).
– Mehrdad Afshari Dec 18 '09 at 18:33 1 Single() will raise an exception if more than a single item exists in the sequence. If you don't care to throw an exception, you could also use First() or FirstOrDefault(). – LBushkin Dec 18 '09 at 18:35 @Mehrdad - I made the change, but is there any performance difference between the two?
I usually prefer . Where(predicate).Single() because I think it expresses my intent a little clearer. – Justin Niessner Dec 18 '09 at 18:36 @Justin: I doubt there's a noticeable performance difference.
The expression tree will be slightly more verbose but the resulting SQL should be similar. SingleOrDefault has a similar overload too. I prefer these since they are more concise.By the way, Single will return the element if it contains exactly one element and throws an exception otherwise.
– Mehrdad Afshari Dec 18 '09 at 18:39 @Justin: You could have a very very minor difference as with the two-method chain you create one extra enumerator object. However, since object creation is pretty fast these days, you'll probably never notice it. – R.
Martinho Fernandes Dec 18 '09 at 18:41.
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.
You can just use .First() or .FirstOrDefault() like so: Foo foo = query. Select(a => a. Name == "foo").FirstOrDefault().
If the query "should only return one result" Single is more effective as it throws if the query returns more than one, flagging bugs. – R. Martinho Fernandes Dec 18 '09 at 18:36 @Martinho Fernandes You are right, I had never used Single – fordan Dec 18 '09 at 20:41.
From all the have have said my addition is the use the Value property after you have a single element if you are using LINQ-to-XML. And the Select new { *cols* } if it a list or array or table. Example.... select new {c.Name, c.
Value}; This tip is to enable you get the values.
Language-Integrated Query (LINQ) makes it easy to access database information and execute queries. By default, LINQ queries return a list of objects as an anonymous type. You can also specify that a query return a list of a specific type by using the Select clause.
The following example shows how to create a new application that performs queries against a SQL Server database and projects the results as a specific named type. For more information, see Anonymous Types (Visual Basic) and Select Clause (Visual Basic). The examples in this topic use the Northwind sample database.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.