Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 1st, 2009, 11:45 AM
Authorized User
 
Join Date: Jan 2009
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Default Selecting Items with Linq

In my database, I have two tables, PhotoAlbum and Picture. I would like to create a gallery page that displays the of the album [PhotoAlbum.Name] along with the first picture [Picture.ImageUrl] from that album. I have tried several things but cannot get the results I want. I know that I want to loop through the Album table and get the name for every album and then loop through the Pictures table and find the first picture for each album, the problem is that I don't know how to code it. Please help.

Code:
 Using myDataContext As MyDataContext = New MyDataContext()
            Dim allAlbums = From r In myDataContext.PhotoAlbums _
                            Where r.Id > 0 _
                            Select New With {r.Name}
 
            Repeater1.DataSource = allAlbums
            Repeater1.DataBind()
  #2 (permalink)  
Old April 2nd, 2009, 07:51 AM
Authorized User
 
Join Date: Jan 2009
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have gotten this far, which displays the photo album with all the pictures, How can I delimit it to the picture with the lowest id?

Code:
Dim allAlbums = From albums In myDataContext.PhotoAlbums, pics In myDataContext.Pictures _
Where albums.Id = pics.PhotoAlbumId _
OrderBy albums.Id _
SelectNewWith {albums.Name, pics.ImageUrl}
  #3 (permalink)  
Old April 2nd, 2009, 03:01 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

There are a few ways to do it. The simplest way is probably to sort the list based on its Id (as you have now) and then use FirstOrDefault to get the first item from the result set:

Code:
 
Dim allAlbums = (From albums In myDataContext.PhotoAlbums, pics In myDataContext.Pictures _
Where albums.Id = pics.PhotoAlbumId _
Order By albums.Id _
Select New With {albums.Name, pics.ImageUrl}).FirstOrDefault()
Notice the extra parentheses around the entire query and the call to FirstOrDefault().

Hope this helps,

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!
  #4 (permalink)  
Old April 2nd, 2009, 06:06 PM
Authorized User
 
Join Date: Jan 2009
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried it with first, but not first or default. It is frustrating when you can think the logic, but can't code it. I tried to implement your suggestion, but now I am getting the following error:

Quote:
An invalid data source is being used for Repeater1. A valid data source must implement either IListSource or IEnumerable.
From what I read, the data source should implement IEnumerable. Any ideas?

Thank you very much for your help.
  #5 (permalink)  
Old April 2nd, 2009, 06:11 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Maybe the Repeater isn't the right control for this? You only want a single item, so there's nothing to repeat.... ;-)

Maybe a DetailsView, a FormView or custom controls are better suited for this task?

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!
  #6 (permalink)  
Old April 3rd, 2009, 06:43 AM
Authorized User
 
Join Date: Jan 2009
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I do want it to repeat because I want to display each album name and for each album name, the picture in that album.
  #7 (permalink)  
Old April 3rd, 2009, 07:12 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Yes, but the outer element is a singular object, isn't it?

E.g. you have a single album entity, with a collection of Pictures, right?

In that case, you should use singular controls for the Album (simple controls like a Label or a TextBox) and a repeating control (like a Repeater or a ListView) for the collection of Pictures.

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!
  #8 (permalink)  
Old April 6th, 2009, 10:00 AM
Authorized User
 
Join Date: Jan 2009
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm sorry, but I don't follow your explaination. I broke the code down to display exactly what I wanted it to, and this is what I have:

Code:
 Using myDatabaseContext As MyDataContext = New MyDataContext()
            Dim allAlbums = (From albums In myDatabaseContext.PhotoAlbums, pics In myDatabaseContext.Pictures _
                        Where albums.Id = pics.PhotoAlbumId _
                        Order By albums.Id _
                        Select New With {albums.Name, albums.Id, pics.ImageUrl}).FirstOrDefault()
 
            albumName.Text = allAlbums.Name.ToString()
            Image1.ImageUrl = allAlbums.ImageUrl
            HyperLink1.NavigateUrl = "~/Members/Photos/Default.aspx?PhotoAlbumId=" & allAlbums.Id
This code produces the first album and the first picture of that album which is turned into a hyperlink. All I need now is this code to repeat for all the albums in the database. Can I do this using a for loop or do I need to go back to the repeater?

Thank you.
  #9 (permalink)  
Old April 6th, 2009, 02:24 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Can't you select the Albums and Pics in one fell swoop, bind the album to single fields and the Pics to a repeater? E.g. something like the following pseudo code:
Code:
 
var album = from a in Context
                  where ....
                  select a.Name, a.Pictures
 
myLabel.Text = album.Name;
myRepeater.DataSource = album.Pics
myRepeater.DataBind();
Hope this helps,

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!
  #10 (permalink)  
Old April 7th, 2009, 12:21 PM
Authorized User
 
Join Date: Jan 2009
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Default

But that brings in the following error with using the repeater because I want to select the first picture as a link to the gallery.

Quote:
Sequence operators not supported for type 'System.String'
That is why I thought a for loop might work best, the problem is I don't know how to do that. What do you think?
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using LINQ for this book... Lee Dumond BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 3 January 9th, 2008 02:05 AM
how to automate selecting all items from list satish_k Pro VB.NET 2002/2003 0 October 3rd, 2006 06:23 AM
Selecting Items From Reports Brendan Bartley Access 3 June 9th, 2006 07:05 AM
displaying 6 items only having 20 items Lakshmi KS VB Components 1 February 17th, 2004 10:34 AM



All times are GMT -4. The time now is 08:57 AM.


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