Get Average Using LINQ

Posted by obautista on Stack Overflow See other posts from Stack Overflow or by obautista
Published on 2011-01-04T04:41:39Z Indexed on 2011/01/04 4:54 UTC
Read the original article Hit count: 221

Filed under:
|

Hoping someone can help me with the LINQ syntax to calculate an average. For example, I have the following LINQ query:

var rates = from rating in ctx.Rates  
            where rating.Id == Id  
            select new 
              {   
                 UserId = rating.UserId,  
                 Rating = rating.Rating  
              };  

If 10 records are returned, I need to calculate average on the Rating field. It is defined as as a Double in my DB. I am using LINQ to EF. So I would be assigning the UserId, MiscId, and the Rating would be the average on the records returned. I am passing one object back to the client code.

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ