MD Ghouse MD Ghouse - 1 month ago 7
C# Question

How to count sum on the day of entering record

Here im fetching record By using datetime.But i want that day Whole informatio Like Sum

Example

ID Name Date Amount
1 Samsung 1/1/2016 3000
2 LG 1/1/2016 6000
3 Videocon 2/2/2015 200
4 Philips 2/2/2015 1500
5 Sony 3/5/2015 15000


If I
Select
date Like
1/1/2016
its should Give me complete
Result
along with
total --> 9000
& also its mentioned as
Samsung-->3000
Lg--->6000
Total 9000

Here I have Some Code

public JsonResult Dif(int Date=0, int Month=0, int Year=0, int HH = 0, int MM = 0, int Ss = 0){
DateTime ss = new DateTime(Year, Month, Date, HH, MM, Ss);
var x = (from n in db.Employees where (n.DataofJoin == ss) select n);
return new JsonResult {Data=x, JsonRequestBehavior = JsonRequestBehavior.AllowGet };}

Answer

Try this:

var total = (from n in db.Employees where (n.DataofJoin == ss) select n.Amount).Sum();

Edit (thanks Mrinal for correction): you may try following syntax:

//Get total
var total = employees.Where(ef => ef.DataofJoin == yourDate).Select(f => f.Amount).Sum();
//get list with total
var result = employees.Where(e => ef.DataofJoin == yourDate).ToList();
//Add object for total amount
result.Add(new employee { name = "Total", Amount = total });