LINQ to SQL maps the relational database into an object model, and when the program runs the queries in the object model are translated into SQL. SQL contains Aggregate functions like SUM, AVG, MIN, MAX and more. Lets see how this functionality can be implemented using LINQ Enumerable standard aggregation operators. You can also create custom Aggregations to implement any other required behavior. The Examples below show how use Average, Count and Sum using the following SQL table:

Average

var query1 = from income in data.Incomes
group income by income.name into result
select new
{
Name = result.Key,
Average = result.Average(i => i.income)
};
foreach (var q in query1)
{
Debug.WriteLine(string.Format("Name: {0} , Average income: {1}", q.Name, q.Average));
}

SELECT AVG([t0].[Income]) AS [Average], [t0].[Name]
FROM [dbo].[Incomes] AS [t0]
GROUP BY [t0].[Name]
...
SELECT SUM([t0].[Income]) AS [Sum], [t0].[Name]
FROM [dbo].[Incomes] AS [t0]
GROUP BY [t0].[Name]
...
SELECT COUNT(*) AS [Count], [t0].[Name]
FROM [dbo].[Incomes] AS [t0]
GROUP BY [t0].[Name]

A novice in trouble …
I tried using the Average function given above. I have a text file from which I have read the data. Now I am trying to get the average using the code below
var marksBySubject = from m in marks
group m by m.subjectName into results
select new
{
subjectName = results.Key,
avgMarks = results.Average(i => i.marksObtained )
};

I get an error “cannot implicitly convert type “string” to “decimal?” on the line where lambda expression is used. I am unable to figure out what is the solution. Help is much appreciated.
Thanks

Hello,
LINQ is great for developers who does not deal with sql database access, querying, etc.
But the syntax of LINQ seems to be complex and I guess must be improved.
But the above samples are great value for Linq learners.