asp.net mvc , raw sql : Display data in Views generated using aggregate function - sql

I am using ASP.NET MVC, EF 6 and SQL Server 2008.
I want to generate a view which would show sum of all the sales in each day for a particular month in a particular year.
I found LINQ query very complicated in such type of job, So I used a raw SQL query. I wrote query and tested in SQL server and it worked fine.
select
YEAR(Date) as Year,
MONTH(Date) as month,
DAY(Date) as date,
SUM(GrandTotal) as Total
from
Sales
where
Year(Date) = 2014
and MONTH(Date) = 12
group by
DAY(Date), YEAR(Date), MONTH(date)
Result
Well currently I don't have much data. But it looks like I got what I wanted from a query.
I wrote a controller for this purpose and now I have no idea how to display this data in View.
public ActionResult MonthlySalesByDate()
{
DateTime today = DateTime.Now.Date;
int _year = today.Year;
int _month = today.Month;
//raw sql query
string query = "select SUM(GrandTotal) as Total, DAY(Date) as date, MONTH(Date) as month, YEAR(Date) as Year from Sales where Year(Date) = " + _year + " and MONTH(Date) =" + _month + " Group by DAY(Date), YEAR(Date), MONTH(date)";
//executing raw sql query
var _model = db.Stocks.SqlQuery(query).ToList();
return View(_model);
}
Please help me out with this. If there is better way of doing this or if I am making mistakes, please let me know.

Start by creating view models to represent what you want to display in the view
public class DayTotalVM
{
public int Day { get; set; }
[DisplayFormat(DataFormatString = "{0:C}")]
public decimal Total { get; set; }
}
public class SalesVM
{
[DisplayFormat(DataFormatString = "{0:MMMM yyyy}")]
public DateTime Date { get; set; }
public List<DayTotalVM> Days { get; set; }
}
The sql query you have can be generated in linq and projected into your view models using
int year = 2014;
int month = 12;
var query = db.Sales.Where(x => x.Date.Year == year && x.Date.Month == month)
.GroupBy(x => x.Date).Select(g => new DayTotalVM
{
Day = g.Key.Day,
Total = g.Sum(x => x.Total)
})
However this will only give you the 2 items as per you above image, but from the comments you want to display all days in the month, so you can add
int daysInMonth = DateTime.DaysInMonth(year, month);
List<DayTotalVM> days = new List<DayTotalVM>();
for(int i = 1; i < daysInMonth + 1; i++)
{
DayTotalVM item = new DayTotalVM () { Day = i };
DayTotalVM ex = query.Where(x => x.Day == i).FirstOrDefault();
if (ex != null)
{
item.Total = ex.Total;
}
days.Add(item);
}
and finally initialize and return your view model
SalesVM model = new SalesVM();
{
Date = new DateTime(year, month, 1),
Days = days
}
return View(model);
And then the view would be
#model SalesVM
#Html.DisplayFor(m => m.Date);
<table>
#for(int i = 0; i < Model.Days.Count; i++)
{
<tr>
<td>#Html.DisplayFor(m => m.Days[i].Day)</td>
<td>#Html.DisplayFor(m => m.Days[i].Total)</td>
</tr>
}
</table>
Edit
The for loop could be replace by using a GroupJoin()
public ActionResult MonthlySalesByDate(int year, int month)
{
int daysInMonth = DateTime.DaysInMonth(year, month);
var days = Enumerable.Range(1, daysInMonth);
var query = db.Sales.Where(x => x.Date.Year == year && x.Date.Month == month).Select(g => new
{
Day = g.Date.Day,
Total = g.Total
});
var model = new SalesVM
{
Date = new DateTime(year, month, 1),
Days = days.GroupJoin(query, d => d, q => q.Day, (d, q) => new DayTotalVM
{
Day = d,
Total = q.Sum(x => x.Total)
}).ToList()
};
return View(model);
}

Related

Count Total issued per month using LINQ

I'm doing a sample project where it counts the total issues per month that is to be displayed in a bar graph..
Here is my working SQL query
SELECT
SUM(CASE datepart(month,D_ISSUE) WHEN 1 THEN 1 ELSE 0 END) AS 'Jan',
SUM(CASE datepart(month,D_ISSUE) WHEN 2 THEN 1 ELSE 0 END) AS 'Feb',
so on...
FROM EMP_MEMOS
Can someone help me transpose this SQL Query into a LinQ code. i'm still trying to understand how it works
Here is my code so far, but i still can't get it to work.
public ActionResult MonthCount()
{
var Monthly = (from f in db.EMP_MEMOS
group f by new { month = f.D_ISSUE, year = f.D_ISSUE } into g
orderby g.Key.year
select new
{
dt = string.Format("{0}/{1}", g.Key.month, g.Key.year),
}).ToList();
return Json(new { result = Monthly }, JsonRequestBehavior.AllowGet);
}
I already got the answer, gonna share it here:
public ActionResult CountMonth()
{
var memo = from t in db.EMP_MEMOS
select new
{
t.D_ISSUE.Value.Month,
t.D_ISSUE.Value.Year
};
var res = from s in memo
group s by new { s.Year, s.Month } into g
select new
{
Period = g.Key,
MemoCount = g.Count(),
};
return Json(new { result = res }, JsonRequestBehavior.AllowGet);
}

datediff with case statement in select query linq

select case statement in linq query.
Here is the query on sql:
select case when DATEDIFF(day,convert(varchar,Min([Order].CreatedOnUtc),101),convert(varchar,Max([Order].CreatedOnUtc),101)) = 0 then
Sum([Order].OrderSubtotal)
else
case when (DATEDIFF(day,convert(varchar,Min([Order].CreatedOnUtc),101),convert(varchar,Max([Order].CreatedOnUtc),101))/30) = 0 then Sum([Order].OrderSubtotal) else
Sum([Order].OrderSubtotal)/
(DATEDIFF(day,convert(varchar,Min([Order].CreatedOnUtc),101),convert(varchar,Max([Order].CreatedOnUtc),101))/30)
end
end as 'Account Value' from [order] where And Account.ID = #Act_ID
I am trying the code here:
var query = _orderRepository.Table;
query = query.Where(o => o.AccountId == accountId);
In query i am getting my value.
After query statement what should i write??
how do i write for case statement using linq???
#Manoj, may be the below code helps you. This sample C# project may solve the problem you have.
using System;
using System.Collections.Generic;
using System.Linq;
namespace DateDiffIssue
{
class Program
{
static void Main(string[] args)
{
// Preparing data
var data = new Order[] {
new Order { AccountID = 1, CreatedOnUtc = DateTime.Parse("1.01.2017 10:00"), OrderSubtotal = 100 },
new Order { AccountID = 1, CreatedOnUtc = DateTime.Parse("1.01.2017 12:00"), OrderSubtotal = 150 },
new Order { AccountID = 1, CreatedOnUtc = DateTime.Parse("1.01.2017 14:00"), OrderSubtotal = 150 }
};
// Selection
var selected = (from item in data
let accountData = data.Where(w => w.AccountID == 1)
let minDate = accountData.Min(m => m.CreatedOnUtc).Date
let maxDate = accountData.Where(w => w.AccountID == 1).Max(m => m.CreatedOnUtc).Date
let isSameDate = minDate == maxDate
let basedOn30Days = (maxDate - minDate).TotalDays / 30
let isInside30Days = (int)basedOn30Days == 0
let accountDataSum = accountData.Sum(s => s.OrderSubtotal)
select new
{
AccountValue = isSameDate ? accountDataSum :
isInside30Days ? accountDataSum :
accountDataSum / basedOn30Days
}).Distinct();
// Print each order
selected.ToList().ForEach(Console.WriteLine);
// Wait for key
Console.WriteLine("Please press key");
Console.ReadKey();
}
}
internal class Order
{
public int AccountID { get; set; }
public DateTime CreatedOnUtc { get; set; }
public int OrderSubtotal { get; set; }
}
}

How to convert this sql part to linq

select sum(DATEDIFF(day,LeaveBreakup.StartDate,LeaveBreakup.EndDate)+1)
what I want is to convert the statement to linq select statement
class LeaveBreakup
{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
List<LeaveBreakup> Dates = new List<LeaveBreakup>();
Dates.Add(new LeaveBreakup(){StartDate = DateTime.Now.AddDays(-3), EndDate = DateTime.Now });
Dates.Add(new LeaveBreakup(){StartDate = DateTime.Now.AddDays(-2), EndDate = DateTime.Now });
LINQ BIT
var Result = (from D in Dates
select (D.EndDate - D.StartDate).TotalDays + 1)
.Sum();
If you want to know the diff without having to worry about having a negative value then wrap the calculation in Math.Abs
var Result = (from D in Dates
select Math.Abs((D.StartDate - D.EndDate).TotalDays) + 1)
.Sum();
In this example your Result is 7

How to select all the days of the week from a given day?

Given a date I want to get all the other days of that same week, where in the week starts and ends on Saturday and Friday.
Model
public TimeModel
{
public int ID
public DateTime Day
}
What I'm currently doing
public Contrller{
private db = new ModelContext();
public AddDates(DateTime Date)
{
List<Model> list = new List<Model>();
int n = 0;
while(Date.DayofWeek != DayofWeek.Sauturday)
Date = Date.AddDats(-1) // keep subracting the date until I reach Saturday
while(Date.DayofWeek != DayofWeek.Friday
{
list.Add(Find(Date));
//Simply put for each date not Friday
// I find the corresponding model (the one with the same date)
//and add it to the list
Date = Date.AddDays(1)
}
list.Add(Find(Date)); // To add the Friday date to list
}
Note: Not exactly my code, just a simplification of my problem.
To summarize my solution:
a) Subtract given date until Saturday
b) Find model which corresponds to Date
c) Repeat until I reach Friday
d) Add to list once more to include Friday
Is it possible to create a linq/sql statement to simpyly select the needed models (with regards to Date)?
You can find a sample implementation that gets the current week.
List<TimeModel> list = new List<TimeModel>();
int n = 0;
for (int i = 0; i < 200; i++)
list.Add(new TimeModel{ID = i, Day = DateTime.Now.AddDays(-i)});
var currentDay = new TimeModel() {ID = 0, Day = DateTime.Now};
var previousSaturday = currentDay.Day.AddDays(-(int)currentDay.Day.DayOfWeek - 1);
var nextFriday = previousSaturday.AddDays(6);
var currentWeek = list.Where(p => p.Day.DayOfYear >= previousSaturday.DayOfYear && p.Day.DayOfYear <= nextFriday.DayOfYear).OrderBy(p => p.Day.DayOfYear).ToList();

Using SQL Server Date datatype and C# DateTime to select records

I have an ActionResult where I want to select records based on a date column in SQL Server. This date is in a column of type Date. I can't directly compare the dates since C# DateTime includes the time component and the Date datatype does not. Is there a nice way to do this?
public ActionResult AbsencesByDate(DateTime date)
{
var absences = from attendance in db.Attendances
where attendance.Date == date
select new
{
returnedPersonID = attendance.PersonID,
FullName = attendance.Person.FName + " " + attendance.Person.LName,
};
return Json(absences, JsonRequestBehavior.AllowGet);
}
You could remove the time part from your date parameter in your function.
Something like this :
public ActionResult AbsencesByDate(DateTime date)
{
date = date.Date;
var absences = from attendance in db.Attendances
where attendance.Date == date
select new
{
returnedPersonID = attendance.PersonID,
FullName = attendance.Person.FName + " " + attendance.Person.LName,
};
return Json(absences, JsonRequestBehavior.AllowGet);
}
try using:
where attendance.Date == date.Date