Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
|
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB 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 March 17th, 2012, 02:26 PM
Authorized User
 
Join Date: Dec 2011
Posts: 26
Thanks: 1
Thanked 4 Times in 3 Posts
Default Having trouble understanding Range Variable

Hello,
I am on Ch. 14 and I seem to be having difficulty understanding the concept of the range variable, what it is, and what it is for.

I saw on another post that you said something like "it references the object within the query" what object?

For example, if this is the code:

var allReviews = from r in myEntities.Reviews
select r;

the object r referes to is the myEntities.Reviews object, correct? So, this kind of works like aliasing a table in SQL?

To make sure I understand: When you "select r" you are just selecting all of the reviews?

Why can't the query just be
var allReviews = from myEntities.Reviews
select r;
?

Did they just have the syntax build that way so that you don't have to type myEntities.Reviews in the WHERE statement when you filter? (So, it is like an alias once again?)


Also, I thought I under stood this and then I hit this in a EntityDataSource control later on.

Where = "it.PhotoAlbum.Id = @photoAlbumId"

The book says "it" is a implicit range variable and that you must use "it" and cannot use your own range variable name. Why? Why must I use "it"? Is "it" always used for implicit range variables? How does the program know what "it" refers to? Does "it" refer to the object built with what is returned from the database when the system queries the PhotoAlbum table for the photos with that photoAlbumId?

Similar to my question above, why can't the code be:

Where = "PhotoAlbum.Id = @photoAlbumId" ?


Sorry for all the questions in one post, but I feel like I am missing something with what a range variable's purpose is and, correspondingly, how it is used.
 
Old March 17th, 2012, 03:59 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:
the object r referes to is the myEntities.Reviews object, correct? So, this kind of works like aliasing a table in SQL?
Nope, not correct. It doesn't refer to the entire collection, but to a single object. As such it's not an alias to the Reviews collection. Think of it as the iteration variable in a for loop. Within the loop you can access the details of the iteration variable.

The same is true for a LINQ query: you use the r in this case to select the item, but also to filter (e.g. Where r.SomeProperty = SomeValue)

Quote:
To make sure I understand: When you "select r" you are just selecting all of the reviews?
In this example, yes:
Code:
 
var allReviews = from r in myEntities.Reviews
select r;
However, with a filter the queries looks like this:

Code:
 
var allReviews = from r in myEntities.Reviews
                        where r.Id > 10
                        select r;
And now it no longer returns all Reviews. r.Id is used here to specify the Where criteria.

Quote:
Did they just have the syntax build that way so that you don't have to type myEntities.Reviews in the WHERE statement when you filter? (So, it is like an alias once again?)
I don't think so. myEntities.Reviews is a collection, so it wouldn't make sense (nor compile) to do something like:

Where myEntities.Reviews.Id > 10

That would give the Reviews collection itself an Id. With the range varaible, you refer to the objects in the collection which in turn can have an Id.

Quote:
Why must I use "it"?
You should ask Microsoft; they hardcoded it to be it ;-)

Quote:
Similar to my question above, why can't the code be:

Where = "PhotoAlbum.Id = @photoAlbumId" ?
Again, it needs to know what object to retrieve that infomation from. The "magic" it variable gives access to that....

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 March 17th, 2012, 04:14 PM
Authorized User
 
Join Date: Dec 2011
Posts: 26
Thanks: 1
Thanked 4 Times in 3 Posts
Default 2 more questions

Thanks, Imar. That helps a lot. 2 questions:

1) "it" is always used for implicit range variables, then, correct?

2) Does it only work when configuring a data-source like a EntityDataSource because then it knows that the "it" refers to the object defined in the EntitySetName, or is such a implicit range variable found elsewhere?
 
Old March 17th, 2012, 04:24 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:
1) "it" is always used for implicit range variables, then, correct?
No, "it" is an Entity Data Source implementation detail, not a LINQ implementation.

Quote:
2) Does it only work when configuring a data-source like a EntityDataSource because then it knows that the "it" refers to the object defined in the EntitySetName, or is such a implicit range variable found elsewhere?
Yes, see more details here: http://msdn.microsoft.com/en-us/library/cc488531.aspx

Don't know about any other names for implicit range variables, but they could be out there. Let me know if you find one ;-)

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!





Similar Threads
Thread Thread Starter Forum Replies Last Post
name target.address to a range variable bluesboytoo Excel VBA 3 October 19th, 2009 02:34 AM
Using month & year variable to get date in range rsearing ASP.NET 2.0 Professional 2 July 5th, 2008 08:30 AM
Trouble Understanding the BLL jachin BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 7 March 13th, 2008 12:23 AM
understanding Variable use imroostercogburn C++ Programming 7 July 28th, 2006 06:15 PM
Defining a Dynamic Range using a variable Alseikhan Excel VBA 0 March 27th, 2006 02:26 AM





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