 |
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
|
|
|
|
|

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

May 24th, 2010, 03:50 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

May 24th, 2010, 11:22 AM
|
|
Authorized User
|
|
Join Date: Sep 2009
Posts: 35
Thanks: 6
Thanked 1 Time in 1 Post
|
|
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.
|
|

May 24th, 2010, 11:44 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Excellent. Would you mind sharing how you fixed it?
Imar
|
|

May 24th, 2010, 12:44 PM
|
|
Authorized User
|
|
Join Date: Sep 2009
Posts: 35
Thanks: 6
Thanked 1 Time in 1 Post
|
|
Quote:
Originally Posted by Imar
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.
|
|

May 24th, 2010, 12:50 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
LINQ is really powerful but man it is a little tricky at times.
|
Couldn't agree more.... ;-)
Imar
|
|
 |