Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to get a count of items in a Linq query for items with the same description. My query looks kind of like this

C#
var dataSource = (from t in context.Tags
                  select new
                  {
                        Name = t.Description,
                        Count = //I dont know how to do  this :/
                  });


Any help would be great,
Thanks
Posted

1 solution

You can use grouping for that. See: http://msdn.microsoft.com/en-us/library/bb896250.aspx[^]

For example:
C#
var dataSource = (from t in context.Tags
                  group t by t.Description into t_group
                  select new
                  {
                        Name = t_group.Key,
                        Count = t_group.Count()
                  });
 
Share this answer
 
v2
Comments
Wonde Tadesse 19-Dec-11 12:06pm    
5+
Wendelius 19-Dec-11 13:59pm    
Thanks Wonde :)
Amir Mahfoozi 19-Dec-11 12:13pm    
+5
Wendelius 19-Dec-11 13:59pm    
Thank you Amir :)
DominicZA 19-Dec-11 15:34pm    
Thank you, +5 and accepted :D

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