Sum of Column using Linq Lambda Expression in ASP.Net

Last Reply 2 months ago By dharmendr

Posted 2 months ago

how to use  this Query in linq Lamda Expression 

Select  sum(AmountPaid) as TotalAmountPaid 
from StudentLedger 
where FeeTypeID in (1,2) and StudentID =639

 

Posted 2 months ago Modified on 2 months ago

Hi ahsan.ali,

Check this example. Now please take its reference and correct your code. Change the code according to your column datatype.

Database

For this example I have used of NorthWind database that you can download using the link given below.

Download Northwind Database

SQL

SELECT SUM(ReportsTo) AS TotalAmountPaid
FROM Employees
WHERE EmployeeID IN (1,2,5,6) and Extension = 428

Output 

TotalAmountPaid
5

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    NorthwindModel.NorthwindEntities entities = new NorthwindModel.NorthwindEntities();
    var id = new[] { 1, 2, 5, 6 };
    string extension = "428";
    Response.Write("TotalAmountPaid : " + entities.Employees.Where(x => id.Contains(x.EmployeeID) && x.Extension == extension).Sum(x => x.ReportsTo));
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim entities As NorthwindModel.NorthwindEntities = New NorthwindModel.NorthwindEntities()
    Dim id = {1, 2, 5, 6}
    Dim extension As String = "428"
    Response.Write("TotalAmountPaid : " & entities.Employees.Where(Function(x) id.Contains(x.EmployeeID) AndAlso x.Extension = extension).Sum(Function(x) x.ReportsTo))
End Sub

Output

TotalAmountPaid : 5