Click here to Skip to main content
15,914,160 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi, I need some one to explain me some code in c#, what does they mean... I am using that but to tell te truth I don understand that
here is the code :
C#
GroupBy(name => name).Select(group => string.Format("{0} [{1}]",group.Key , group.Count()))

string.Format("{0} [{1}]  --- ???  I have the result  e.x  Europe[4] 

so can explain any one, what does it mean "{0} ande [{1}] " thank you

gorup.key ---?
group.Count----?
Posted
Updated 26-May-12 1:33am
v3
Comments
Philippe Mori 26-May-12 13:25pm    
I think you can at least read the help on a function before asking trivial questions.

{0} and {1} are parameters to the string.Format format string, which are replaced by the first and second parameters following the format string when constructing a new string: in this case, by "group.Key" and "group.Count()" respectively. The '[' and ']' characters are just literal characters copied directly to the output string - as you can see in your print. There are 4 "Europe" items.

"group" is the name given to each item in the enumerable list returned by the GroupBy Linq extension method.
Think of it as:
C#
foreach (var group in GroupBy(name => name))
   {
   Select(string.Format("{0} [{1}]",group.Key , group.Count()));
   }
It isn't done like that, and that wouldn't compile anyway, but it gives you an idea what is going on.
 
Share this answer
 
Comments
Shahin Khorshidnia 26-May-12 11:54am    
My + Five
Nelek 26-May-12 17:44pm    
Nice explanation
Can You Specify what is GroupBy whether it is DataTable or Dictionary or some thing else

As for the string format it will concatenate the string what ever you give as a parameter.Those brackets are just the position in what popsition what text should come so 0'th position group.key value will come and at {1} object count will come
 
Share this answer
 
To understand LINQ I would look at the following article: How does it work in C#? - Part 3 (C# Linq in detail)[^]
 
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