I have this tSQL query that is returning what I need to see from the data.
select AppName, MethodName, sum(ElapsedMilliseconds)/count(MethodName) as avgMS
from [dbo].[AppMethodPerformance]
where YEAR(MethodDateTime) = 2023 and DatePart(week, MethodDateTime) = 4
group by appname, MethodName
order by AppName, MethodName
I need this in linq method syntax to work as close as possible
Ive been trying these two attempts but im not getting anywhere.
List<AppMethodPerformance> allEntires = new();
using (var ctx = new AppLogContext(clientName))
{
//allEntires = await ctx.AppMethodPerformances
// .Select(s => new { s.ElapsedMilliseconds, s.AppName, s.MethodName, s.MethodDateTime})
// .Where(s => s.MethodDateTime.Year == year && ISOWeek(s.MethodDateTime) == week)
// .GroupBy(s => new { s.AppName, s.MethodName, s.ElapsedMilliseconds } ).Sum(x => x.Key.ElapsedMilliseconds)
// .Select(s =>
// new AppMethodPerformance
// {
// AppName = s.Key.AppName,
// MethodName = s.Key.MethodName,
// ElapsedMilliseconds = s.Key.ElapsedMilliseconds
// })
// .OrderBy(s => s.AppName).ThenBy(s => s.MethodName)
// .ToListAsync();
allEntires = await ctx.AppMethodPerformances
.Where(s => s.MethodDateTime.Year == year && ISOWeek(s.MethodDateTime) == week)
.GroupBy(s => new
{
s.AppName,
s.MethodName,
s.ElapsedMilliseconds
})
.Select(s =>
new AppMethodPerformance
{
AppName = s.Key.AppName,
MethodName = s.Key.MethodName,
ElapsedMilliseconds = s.Key.ElapsedMilliseconds
})
.ToListAsync();
}
I also realize that now that ISOweek method will not work inside EF because its not convertible to sql. So ill need to find another way to match with the week of the year. I need some direction on how to best tackle this.
ref
private static int ISOWeek(DateTime methodDateTime)
{
CultureInfo myCI = new CultureInfo("en-US");
Calendar myCal = myCI.Calendar;
// Gets the DTFI properties required by GetWeekOfYear.
CalendarWeekRule myCWR = myCI.DateTimeFormat.CalendarWeekRule;
DayOfWeek myFirstDOW = myCI.DateTimeFormat.FirstDayOfWeek;
return myCal.GetWeekOfYear(DateTime.Now, myCWR, myFirstDOW);
}