Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
hi I have this code but in fact I dont understant what happen here can any one explain me the code :
ordering.GroupBy(name=> name).OrderByDescending(g => g.Count())
I dont understand (name=>name) and (g=>g.count) help me only in this part
Posted 4-Jun-12 9:32am
h5h5342
Edited 4-Jun-12 10:16am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

You might understand this better by the following
 
name => name is the same as f(name) = name, just that you do not need to include a function name (in this care the function name is f)
 
Therefore the first ordering.GroupBy(name => name) is saying that you want the grouping to be according the the name. Now this code creates a key that in this case only has the name and a collection of records (objects) that have the same name.
 
What is happening in the second part is that you are counting the records under each record which is a collection of original records that have the same key, or in this case are the same string. As a function this would be f(keyCollection) = keyCollection.Count().
 
This might help:
 
Define a class:
 
public class t
{
    public string Shortname { get; set; }
    public string Longname { get; set; }
}
 
Then put this in a console program:
 
var testclass = new t[] {new t {Shortname = "US", Longname = "Long US 1"},
    new t {Shortname = "US", Longname = "Long US 2"},
    new t {Shortname = "UK", Longname = "Long UK 1"},};
 
var testLinq = testclass.GroupBy(i => i.Shortname);
 
foreach (var g in testLinq)
{
    Console.WriteLine("Key = " + g.Key);
    Console.WriteLine("  Records");
    foreach (var r in g)
        Console.WriteLine("   " + r.Longname);
    Console.WriteLine();
}
 
Console.ReadLine();
 
You will get the following:
 
Key = US
  Records
   Long US 1
   Long US 2
 
Key = UK
  Records
   Long UK 1
  Permalink  
v2
Comments
Sandeep Mewara at 5-Jun-12 11:14am
   
5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

This is a nice article How does it work in C#? - Part 3 (C# LINQ in detail)[^]
 
Please read it and it will help you.
 
Thanks
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Those are lambdas,
 
name => name
Takes name as an argument and returns name
 
g => g.count
Takes g as an argument and returns g.Count()
 
So it groups them by name, then orders them by the number of items in each group.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

this syntax call lambda Expression!
Read this article about lambda Expression syntax[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You need to take a look at the topic LINQ[^]
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,327
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,920


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 4 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100