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 April 26th, 2010, 08:34 PM
Authorized User
 
Join Date: Sep 2009
Posts: 35
Thanks: 6
Thanked 1 Time in 1 Post
Default

Imar,

I was wondering if you could help me with getting only the first record that matches all of the values in this bit of code:

Code:
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Using myDataContext As New PrimaryLINQDataContext


      Dim firstPicture = From p In myDataContext.Pictures, r In myDataContext.Resorts _
                         , l In myDataContext.Listings Where p.ResortID = r.ResortID _
                         And l.ResortID = r.ResortID AndAlso l.HotProp = True _
                         Select l.SalePrice, l.RentPrice, r.ResortName, p.ImageUrl



      ListView1.DataSource = firstPicture
      ListView1.DataBind()

    End Using


  End Sub
End Class
When I try to add:

Code:
      ListView1.DataSource = firstPicture.First()
      ListView1.DataBind()
I am getting the error: Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource.

And I understand that it is because when I do the .First() it is bringing only 1 record rather than a collection but how can I get it to display the first record for each instance that is true so if I have multiple images it won't display them both with repetitive info?
 
Old April 27th, 2010, 02:34 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Take another look at the query I posted earlier:

Code:
from i in myContext.PhotoAlbums
                                                 select new
                                                 {
                                                    i.Id,
                                                    i.Name,
                                                    ImageUrl = i.Pictures.First().ImageUrl
                                                  };
Here, First() is used in the projection; e.g. when building the annonymous object. The query returns a collection of anonymous objects, each having properties called Id and Name, and an ImageUrl that contains the URL to the first Image of the Pictures collection of the range variable i.

You need to do something similar; e.g. query pictures from the Pictures collection (or whatever it's called) on your range variable and use First there; not on the entire returned collection or you'll indeed get a single instance only.

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 April 27th, 2010, 08:02 PM
Authorized User
 
Join Date: Sep 2009
Posts: 35
Thanks: 6
Thanked 1 Time in 1 Post
Default

Imar, when I am using your method and changing my code from p.ImageUrl to .ImageUrl = p.ImageUrl.First() I get a error on Listview1.Databind() stating 'Sequence operators not supported for type 'System.String''

I am not sure why it works for you but I cannot get it to work.. I've looked in your book and I am using how the anonymous type is put out for VB.NET but it doesn't seem to be working.
 
Old April 28th, 2010, 02:03 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

In my example, i.Pictures is a *collection* of pictures so you can query the first using First. In your case, it seems that p.ImageUrl is the ImageUrl string property of a single p instance. E.g. you just have a single string there, so First doesn't make sense.

I feel we're stuck in a loop and are heading for a discussion similar to the one we had on page 1: Using LINQ a Listview to display an image ;-)

In other words: I don't know your database, your model, your application so I cannot recommend anything useful.

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 April 28th, 2010, 09:36 AM
Authorized User
 
Join Date: Sep 2009
Posts: 35
Thanks: 6
Thanked 1 Time in 1 Post
Default

Quote:
Originally Posted by Imar View Post
In my example, i.Pictures is a *collection* of pictures so you can query the first using First. In your case, it seems that p.ImageUrl is the ImageUrl string property of a single p instance. E.g. you just have a single string there, so First doesn't make sense.

I feel we're stuck in a loop and are heading for a discussion similar to the one we had on page 1: Using LINQ a Listview to display an image ;-)

In other words: I don't know your database, your model, your application so I cannot recommend anything useful.

Imar
Imar,

I'm just a little confused, even when reading the book about the Anonymous types, how exactly I can create an instance of Pictures in the table. Could you just explain that a little based on your example rather than mine since you know that DB?

I'll spend some more time reading the book and looking at the source for it while I wait for your response, but I think I can apply exactly what you have to my case if I could just understand it a little better.

I apologize for not being clear with my example, but I've come to realize it is probably better we work off yours since I've read the book and you wrote it.. so we both can reference that as middle ground we both can reference and understand.

Thank and sorry
 
Old April 28th, 2010, 11:33 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

No need to be sorry; this can be confusing stuff..... ;-)

OK, take another look at my query:

Code:
 
from i in myContext.PhotoAlbums
select new
{
  i.Id,
  i.Name,
  ImageUrl = i.Pictures.First().ImageUrl
};
To understand how this works, you need to consider the data model. There's an PhotoAlbum table with a few properties like Name. There's also a Pictures table with a PhotoAlbumId property that relates back to the Id property of the PhotoAlbum table. In other words, there's a relationship between these two tables where multiple pictures can belong to the same album when they share the same PhotoAlbumId. In terms of SQL statement, you normally would use an INNER JOIN between twese two tables to get related data. LINQ to SQL shields you from this, and magically exposes a Pictures collection on a PhotoAlbum ninstance. Let's take a look at this in a few steps:

Code:
 
PhotoAlbum myAlbum = (from p in myContext.PhotoAlbums
                      select p).FirstOrDefault()
This selects the first available PhotoAlbum.using FirstOrDefault. Now, consider this:

Code:
 
var pictures = from p in myAlbum.Pictures
                      select p
This selects all the pictures from the myAlbum album.

Let's say you don't want the the entire Picture object, but just the ImageUrl property. This is where you can use a projection and query just the stuff you need into an anonymous object:
Code:
 
var pictures = from p in myAlbum.Pictures
                      select new 
                    {
                       p.ImageUrl
                    }
The "select new" statement results in a new, anonymous object (because you didn't define a type with an ImageUrl property and use that) that contains just the URL of the image. The variable pictures in turn holds a collection (an IEnumerable or an IQueryable) wiith objects of the anonymous type. In other words, it contains zero, 1 or more anonynous objects where each object has an ImageUrl property.

Back to the original query:

Code:
 
from i in myContext.PhotoAlbums
select new
{
  i.Id,
  i.Name,
  ImageUrl = i.Pictures.First().ImageUrl
};
For each album in the database, this queries an anonymous object with the following properties:

1. The Id of the album
2. The name of the album
3. The ImageUrl of the first picture from the Pictures collection of the selected album.

This could look like:

{ Id = 1, Name = "Some Name", ImageUrl = "FirstImageOfFirstAlbum.png }
{ Id = 2, Name = "Some Other Name", ImageUrl = "FirstImageOfSecondAlbum.png }
{ Id = 3, Name = "Yet another name", ImageUrl = "FirstImageOfThirdAlbum.png }

Does this help?

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!
The Following User Says Thank You to Imar For This Useful Post:
digink (April 30th, 2010)
 
Old April 30th, 2010, 01:42 PM
Authorized User
 
Join Date: Sep 2009
Posts: 35
Thanks: 6
Thanked 1 Time in 1 Post
Default

Imar, I think I might finally understand what you were getting at!

Essentially what is probably the best course of action is to query the table that all of the other tables tie back to (FK relationship wise), and LINQ will help get the info out of the other tables where needed.

Great I will give this a whirl, it makes a lot more sense.

Thanks again for posting such a clear and descriptive response, hope I wasn't too frustrating to deal with!
 
Old April 30th, 2010, 08:45 PM
Authorized User
 
Join Date: Sep 2009
Posts: 35
Thanks: 6
Thanked 1 Time in 1 Post
Default

Update:

Imar,

Just wanted to let you know after your explaination above my code is working beautifully! I finally got the anonomyous types down and my problem from the start was I wasn't querying the main table that ties the rest of my database together and once I did that everything is working beautifully. With a little bit of CSS I will be able to get this control to look excellent and work just as intended.

Thanks for all of your help!
 
Old May 1st, 2010, 04:22 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
hope I wasn't too frustrating to deal with!
Absolutely not. You bought a Beginners book, so it makes perfect sense to me that you can't digest all of this at the first try .... ;-)

Quote:
Just wanted to let you know after your explaination above my code is working beautifully! I
Excellent, and thanks for the update. Would you mind sharing your final query code?

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 1st, 2010, 12:23 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
Absolutely not. You bought a Beginners book, so it makes perfect sense to me that you can't digest all of this at the first try .... ;-)

Excellent, and thanks for the update. Would you mind sharing your final query code?

Cheers,

Imar
Sure, basically what I was doing wrong was using a secondary table to try to get information from the primary and another secondary table. After your explanation I realized I need to query the table that is at the center of both of the secondary tables because from there I will be able to get all the related information based upon the PK of the primary table which is the FK on each of my secondary tables.

The code ended up looking like this:

Code:
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Using myDataContext As New PrimaryLINQDataContext


      Dim firstPicture = From r In myDataContext.Resorts _
                         Where r.ResortID = r.Listings.FirstOrDefault.ResortID _
                         And r.Listings.FirstOrDefault().ResortID = _
                         r.Pictures.FirstOrDefault().ResortID _
                         Select New With {r.ResortName, _
                                          .SalePrice = FormatCurrency(r.Listings.First().SalePrice, 0), _
                                          .RentPrice = FormatCurrency(r.Listings.First().RentPrice, 0), _
                                          r.Pictures.First().ImageUrl}



      ListView1.DataSource = firstPicture
      ListView1.DataBind()

    End Using


  End Sub
End Class
It took me a while to figure out what you were doing with having the ImageUrl = i.Pictures.First().ImageURL you posted above but once it made sense this will now pave the way for another piece of code that I need to write. I realize how powerful LINQ is now and how easy it will make my life with this current project!

Thanks again

Last edited by digink; May 1st, 2010 at 03:31 PM..
The Following User Says Thank You to digink For This Useful Post:
Imar (May 1st, 2010)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Upload image-create & save thumbnail-display image angshujit ASP.NET 2.0 Professional 6 July 11th, 2013 10:34 PM
help with image display dhillon555 ASP.NET 1.0 and 1.1 Basics 21 February 19th, 2007 03:32 PM
drag and drop image from listview to Word jacklin General .NET 0 August 14th, 2006 01:12 AM
Display ListView click value Intel.Leiria Access VBA 0 September 27th, 2005 03:09 PM
How to Display the image from the image field. mikeparams SQL Server 2000 1 October 29th, 2004 02:23 AM





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