Click here to Skip to main content
15,904,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the given link below, I couldn't understand the example that is given by them.
http://msdn.microsoft.com/en-us/library/bb534675.aspx[^]

I looked into this link to have a basic idea about Join in Object Context, and my question is

C#
var query = petsList.GroupBy(
                  pet => Math.Floor(pet.Age),
                  (age, pets) => new
                  {
                      Key = age,
                      Count = pets.Count(),
                      Min = pets.Min(pet => pet.Age),
                      Max = pets.Max(pet => pet.Age)
                  });

(example that is used in the link)
In the above, What does age and pets represent?
Posted
Comments
ridoy 13-Sep-13 3:37am    
i don't find given code inside the link,check that if you forgot to mention the correct link! That is an example of Join,not a GroupBy.
Prasaad SJ 13-Sep-13 3:59am    
Hi ridoy,
The link is working fine. The code is under Examples. If you search for "query" you could easily find that.
BillWoodruff 13-Sep-13 4:27am    
The link you give is to an example of using Join, but your code is taken from another page on MSDN about the 'GroupBy operator:

Enumerable.GroupBy<tsource,> Method (IEnumerable<tsource>, Func<tsource,>, Func<tkey,>, TResult>): [^].

Please edit your question to clearly indicate if it is asking about 'Join, or 'GroupBy, and put in the correct link.

'GroupBy is not the same as 'Join..

One of the best ways to understand complex Linq operations like this one, is to copy the code, execute, and examine the results. Experiment with it, change some of the parameters. Explore it. Experience + theory = competence.
Prasaad SJ 13-Sep-13 5:45am    
Sorry the correct link is
http://msdn.microsoft.com/en-us/library/bb549393.aspx

1 solution

age and pets in (age, pets) => new are the parameter names of the two parameters passed to the anonymous method (or lamba).

The GroupBy method takes two lambas, the first one identifies the key to group on, the second one takes that key (age in the example above) and the items that fall within that group (pets) and allow the caller to do something with them. In this case that "something" is creating a new instance of an anonymous type containing four properties;
Key; The group key, or age of the pets.
Count; The number of pets of this age.
Min; The age of the youngest pet in the group.
Max; The age of the oldest pet in the group.

Hope this helps,
Fredrik
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900