• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 636
  • Last Modified:

Linq GROUPBY

I'm using the groupby clause to compute averages. I'm just having problem with the syntax, hopefully someone can help me.

If you look at the code, the system nicely groups the data together and I can see by printing out the the group.Key values into a string. However it gives me an error when trying to group the group.Average() together. Obviously it does not know that it should average out keyerDuration. How do I state in the code to sort by the keyerDuration field?

It has to be something like mygroup.Average(reference to group.keyerRecord.keyerDuration)
var query = from keyerRecord k in arrylstPace
where keyerDuration > 0
group k by k.KeyerName;

foreach (var mygroup in query)
{
            
lblQuestion3Answer.Text += mygroup.Average() + " " + mygroup.Key;
        
}

Open in new window

0
faqss
Asked:
faqss
  • 2
1 Solution
 
jasonduanCommented:
try

lblQuestion3Answer.Text += mygroup.Average(o => o.keyerDuration) + " " + mygroup.Key;
0
 
faqssAuthor Commented:
what is the o?
0
 
oxyooCommented:
It is a lambda [1] expression (an anonymous function), what is on the left hand side of the lambda operator => (o in this case but could be named anything you like) will specify the input parameters, and on the right is the expression. In this example "o" represents every item in mygroup.

Hope that helps!

 [1]: http://msdn.microsoft.com/en-us/library/bb397687.aspx
0
 
faqssAuthor Commented:
thanks i had no idea how to get to the records within the group.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now