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 May 23rd, 2010, 10:34 PM
Authorized User
 
Join Date: Sep 2009
Posts: 35
Thanks: 6
Thanked 1 Time in 1 Post
Default LINQ Query returning only 1 record

I was just wondering what I could use to get ALL records returned that meet the where clause in this LINQ query, whenever I try to take out the FirstOrDefault() it gives me an error and doesn't seem to work properly without it.

With it it just shows the first record that meets those criteria when I'd like all of them to be displayed.

Any help would be greatly appreciated.

Code:
        Dim firstRecord = From r In myDataContext.Resorts _
                     Where r.ResortID = r.Listings.FirstOrDefault.ResortID _
                     And r.Listings.FirstOrDefault().ResortID = _
                     r.Pictures.FirstOrDefault().ResortID _
                     And r.Country = sCountry And r.State = sState _
                     And r.City = sCity _
                     Order By r.Listings.FirstOrDefault.SalePrice Ascending _
                     Select New With {r.ResortName, _
                                      .SalePrice = FormatCurrency(r.Listings.First().SalePrice, 0), _
                                      .RentPrice = FormatCurrency(r.Listings.First().RentPrice, 0), _
                                      r.Pictures.First().ImageUrl, _
                                      .ListingID = "~/ViewListing.aspx?ListingID=" & r.Listings.FirstOrDefault.ListingID & "&ResortID=" & r.ResortID}



        ListView1.DataSource = firstRecord
        ListView1.DataBind()
A little background, everything links back to the Resorts table via ResortID (two other tables which can be seen are Pictures and Listings).

Thanks!
 
Old May 24th, 2010, 03:50 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

First of all, since this is not related to the book, can you please post this in a more appropriate forum such as the LINQ forum: http://p2p.wrox.com/linq-280/ or a general ASP.NET 3.5 forum: http://p2p.wrox.com/asp-net-3-5-436/

That said, there are three FirstOrDefault calls in your query so it's not really clear which one you would like to remove. It's also pretty unclear what you're trying to achieve, how things are related and what you want as a final result for the query and what you want to display.

In other words: can't help you here; except for a pointer to the 101 LINQ samples that show, among other things, how to use JOINs which is what you probably need: http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old May 24th, 2010, 11:22 AM
Authorized User
 
Join Date: Sep 2009
Posts: 35
Thanks: 6
Thanked 1 Time in 1 Post
Default

Sorry about that Imar,

I checked out that link you posted regarding the LINQ queries and I found what I needed to get it working properly.

Thanks again.
 
Old May 24th, 2010, 11:44 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Excellent. Would you mind sharing how you fixed it?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old May 24th, 2010, 12:44 PM
Authorized User
 
Join Date: Sep 2009
Posts: 35
Thanks: 6
Thanked 1 Time in 1 Post
Default

Quote:
Originally Posted by Imar View Post
Excellent. Would you mind sharing how you fixed it?

Imar
Sure, the updated query looks like this:

Code:
        Dim firstRecord = From r In myDataContext.Resorts, l In r.Listings _
                          Where r.ResortID = l.ResortID And l.ResortID = r.Pictures.FirstOrDefault().ResortID _
                          And r.Country = sCountry And r.State = sState And r.City = sCity _
                          Order By l.SalePrice Ascending _
                          Select New With {r.ResortName, _
                                      .SalePrice = FormatCurrency(l.SalePrice, 0), _
                                      .RentPrice = FormatCurrency(l.RentPrice, 0), _
                                      r.Pictures.FirstOrDefault().ImageUrl, _
                                      .ListingID = "~/ViewListing.aspx?ListingID=" & l.ListingID & "&ResortID=" & r.ResortID}



        ListView1.DataSource = firstRecord
        ListView1.DataBind()
Essentially since the only record I needed the FirstOrDefault of was the picture (can have multiple pictures per resort) it is the only thing I applied that to. For the listing I just had it set with the FK how it shows on the MSDN site and everything is now working beautifully.

LINQ is really powerful but man it is a little tricky at times.
 
Old May 24th, 2010, 12:50 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
LINQ is really powerful but man it is a little tricky at times.
Couldn't agree more.... ;-)

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
LINQ Query modification jsymons BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 12 October 14th, 2009 04:46 PM
How to get access to value in LINQ query result StevenF ASP.NET 3.5 Basics 6 May 12th, 2009 05:46 AM
Binding IEnumerable result set of a LINQ query prakashbpl .NET Framework 3.5 0 November 11th, 2008 04:41 AM
Snapshot recordset returning only 1 record probitaille Access VBA 2 September 6th, 2006 06:16 PM
Returning record in stored function with ADO nmbarbillo Oracle 0 June 15th, 2004 12:36 PM





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