Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 27th, 2008, 11:12 AM
Authorized User
 
Join Date: Jun 2008
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default "Single and SingleOrDefault" (p. 436)

Hi Imar,

Could you please explain why omitting the Single() function from the query

var review22 = (from r in myDataContext.Reviews
                where r.Id == 22
                select r).Single();

would result in multiple returns of the same element?

Also, what factor would determine the number of such returns?

Thank you, as always, for your trouble.

With best regards,

Roman
 
Old June 27th, 2008, 01:29 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The difference between this:
Code:
var review22 = (from r in myDataContext.Reviews
                where r.Id == 22
                select r).Single();
and this
Code:
var review22 = from r in myDataContext.Reviews
                where r.Id == 22
                select r;
is that the second example is returning a collection of items while the first returns a single instance. You could certainly use the first, but you'll need to ask for the first item from the collection.

-Peter
compiledthoughts.com
 
Old June 29th, 2008, 03:43 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

As Peter pointed out, this:

var review22 = from r in myDataContext.Reviews
               where r.Id == 22
               select r;

could return multiple records. You and I (and the database) know that the Id is unique so it can only return one item. However, LINQ doesn't know that. The "where" criteria could easily be something that returns multiple records (like where myClient.City = "Amsterdam" for example).

By using Single() you indicate you know only one item will be returned, so LINQ gives you exactly one item, and not a collection with only one item in it.

Finally, SingleOrDefault is useful if you're not sure if your query returns a record. When there isn't an item with an Id of 22, Single() will crash whereas SingleOrDefault will return null when the item could not be found.

Hope this helps,

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old July 3rd, 2008, 07:48 PM
Authorized User
 
Join Date: Jun 2008
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you, Imar and Peter. Very helpful. ~Roman





Similar Threads
Thread Thread Starter Forum Replies Last Post
Single Login rajuru Beginning PHP 0 September 1st, 2005 08:48 PM
Single crmpicco Excel VBA 2 May 10th, 2005 03:57 AM
ASP.Net Single Single-on with Oracle Application S guhanath Oracle 0 October 6th, 2004 05:05 AM
Table join problem Chapter 12, p 436 PHP-Beg tomasz BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 2 August 31st, 2003 12:56 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.