How to perform SUM operation in Entity Framework

asp.net-mvc asp.net-mvc-4 entity-framework entity-framework-6

Question

I have a table.

create table tblCartItem(
pkCartItemId int primary key identity,
CartId int not null,
ProductId int not null,
Quantity int not null,
Price nvarchar(15)
)

and I want to perform sum opeartion on that like as

Select SUM(Price) from tblCartItem where CartId='107'

and I am trying to following code but its not working

ObjTempCart.CartTotal = (from c in db.tblCartItems where c.CartId == cartId select c.Price).Sum();

Any one help me to do this using Entity Framework. I am using MVC 4 Razor.

1
5
8/13/2016 10:46:54 AM

Popular Answer

May be You can use lambda Expression

var total=db.tblCartItems.Where(t=>t.CartId == cartId).Sum(i=>i.Price);
4
7/3/2017 12:27:15 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow