Get the sum of column using Entity Framework in ASP.Net

Last Reply one year ago By dharmendr

Posted one year ago

hello i'm trying to implement the following sql statement using entity framework 


select coalesce(SUM(cdin_ActMortgageAmnt),0) from CRM.dbo.CDIndex,  where comp_companyid=cdin_companyid and comp_idcust like '%10319%' and cdin_Deleted is null and cdin_startunstufdate is not null and cdin_Status='InProgress'  


Company c = db.Companies.Find(750);  
      var CGP = (from cd in db.CDIndexes  
                 join com in db.Companies on cd.cdin_CompanyId equals com.Comp_CompanyId  
                 com.Comp_IdCust == c.Comp_IdCust &&  
                 cd.cdin_Deleted == null &&  
                 cd.cdin_startunstufdate == null &&  
                 cd.cdin_Status == "InProgress"  
                 select new  
      var query = CGP.Sum(x => x.act);  
      lblSum.Text = query.ToString();  

but when i trace the code var query gives me null ? Please help 

Posted one year ago

Hi fares,

Refer the below sample code. Here i have used Northwind database.

using (NorthwindEntities entities = new NorthwindEntities())
    var CGP = (from customer in entities.Orders
                select customer).ToList();
    var query = CGP.Sum(x => x.Freight);
    lblSum.Text = Math.Round(Convert.ToDecimal(query), 2).ToString();