Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
First of all i am asking so gladly here because i am getting very helpfull "Explanation"
the question is about Key in anonymous-types :

in my code if i debug on 'query' i will see :
[0] = { GetKey = { Size = "18 ZOLL", Colour = "ACTION" }, GetCount = 1 }

then i will get GetCount = 1
and on GetKey i will see
GetKey = { Size = "18 ZOLL", Colour = "ACTION" }


I need to understand what a Key because with another code i got like puzzled about key ?
a good explanation based on this Example it will help me a lot .
thanks always in advanced

What I have tried:

var query = (from ph in _webshopDbContext.FXXLPurchaseHeaders
                         join pl in _webshopDbContext.FXXLPurchaseLines on ph.No_ equals pl.Document_No_
                         where ph.Ship_to_Name_2 == "Zentrallager" && pl.Brand == "CUBE"
                          && ph.Order_Date >= minDateInclusive
                          && ph.Order_Date < maxDateExclusive
                         group pl by new
                         {
                             pl.Size,
                             pl.Colour
                         }
                         into NewGroupResult
                         select new 
                         {
                            GetKey = NewGroupResult.Key,
                            GetCount = NewGroupResult.Count()

                         });
Posted

1 solution

When you call GroupBy, usually you get a series of groups
C#
IEnumerable<IGrouping<key, IEnumerable<T>>>

where each Grouping has a Key used to create the group
Also has an IEnumerable<t> of whatever items are there in your original data set.

e.g.
we have a customer class with Name, number of order and number of products

CustomerInfo(name, ordersCount, productsCount)

sample data

joe 1 5
jane 4 9
john 2 8
jim 3 1
jean 1 3
jill 2 5
jeb 3 3
jenn 4 7
C#
foreach(var item in data.GroupBy(i => i.OrdersCount)
                        .Select(group => new { 
                             Key = group.Key, 
                             Count = group.Count() 
                        }).OrderBy(x => x.OrdersCount)
{
     Console.WriteLine($"Key : {item.Key} Count : {item.Count}");
}

(Group 1): [joe 1 5, jean 1 3]
(Group 2): [john 2 8, jill 2 5]
(Group 3): [jim 3 1, jeb 3 3]
(Group 4): [jane 4 9, jenn 4 7]

In your case you're setting two keys to identify the record like a composite key
C#
group pl by new
{
    pl.Size,
    pl.Colour
}

that's the reason your key look like this
Key = { Size = "18 ZOLL", Colour = "ACTION" }

So it depends on the structure & data that you're trying to group by, accordingly you'll have a key and subset
C#
IEnumerable<T>
of super set of that data.

I hope it'll clear your doubts :)
 
Share this answer
 
Comments
[no name] 2-Oct-19 3:28am    
I think yes . i just need to do some more practices. I thank you
dnxit 2-Oct-19 14:49pm    
I'm happy to help

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