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

faqssAsked:
Who is Participating?
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.