Posted on 2011-04-28
Last Modified: 2013-12-16
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

Question by:faqss
    LVL 11

    Accepted Solution


    lblQuestion3Answer.Text += mygroup.Average(o => o.keyerDuration) + " " + mygroup.Key;

    Author Comment

    what is the o?
    LVL 10

    Expert Comment

    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!


    Author Comment

    thanks i had no idea how to get to the records within the group.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now