LINQ Query - How i can make my query better? -
i have written following linq query return list , iterating list separately convert time hours , taking sum of hours each list item.
i sure might not right away send 3 database calls.
can re-write in better way e.g. groupby or other way assign data model, converting idletime hours , taking sum of idletime??
model class
public class testmodel { public string tasksummary { get; set; } public string locationname { get; set; } public float idletime { get; set; } public string description { get; set; } public float idletimesum { get; set; } public guid taskid { get; set; } }
linq query
list<testmodel> list = _context.time .include(x => x.report) .include(x => x.report.task) .include(x => x.report.task.location) .where(x => taskids.contains(x.report.taskid)) .select(x => new testmodel { tasksummary = x.report.task.summary, locationname = x.report.task.location.name, idletime = x.duration, description = x.description, taskid = x.report.taskid, }).tolist();
how converting hours?
foreach (var item in list) item.idletime = item. idletime / 60;
how taking sum?
foreach (var item in list) item.idletimesum = item. idletime;
just add projection:
.select(x => new testmodel { tasksummary = x.report.task.summary, locationname = x.report.task.location.name, idletime = x.duration / 60.0f, description = x.description, taskid = x.report.taskid, npthourssum = x.duration / 60.0f, }).tolist();
although since you're not showing sum suspect there's more problem that.
Comments
Post a Comment