mmssaann
mmssaann

Reputation: 1507

Aggregate function on Linq to SQL result

I need to calculate average grade for all my dealers.

My results are retrieved using Linq as below:

var gradeData = (from data in oAngieCtxt.prc_ShopInstanceCustomersData(Convert.ToInt32(this.ShopInstanceID), 10000, false)
                         .Where(row => row.RecievedPoints != "n/a")
                        .GroupBy(row => new { row.Name })
                        .Select(g => new
                        {
                            TotalPoints = g.Sum(x => Convert.ToDouble(x.RecievedPoints) * (x.Weightage.ToString() == "0.00" ? 1 : Convert.ToDouble(x.Weightage))),
                            Name = g.Key.Name
                        })
                         select data).ToList();

Now, I want avg = (sum of all TotalPoints)/gradeData.Count

The problem is I am unable to get sum of all TotalPoints from gradeData.

How can I achieve this without using foreach?

Upvotes: 0

Views: 900

Answers (1)

user2486535
user2486535

Reputation: 428

you can use "sum" aggregate function.

int sum = gradeData.sum(O => O.TotalPoints);

Upvotes: 1

Related Questions