I have 2 tables as below:
Downtime
DowntimeCode
I am then doing this to get the Description field from the DowntimeCode table for the DowntimeCode with the sum highest value in the Downtime table for the current date and a specified line:
public string TopCodeForToday(string line)
{
var date = DateTime.Now;
using (var db = new PillowContext())
{
var qry = (from d in db.DownTimes
where DbFunctions.TruncateTime(d.DateTime) == DbFunctions.TruncateTime(date) && d.Line == line
group d by new { d.Code }
into g
let total = g.Sum(x=>x.Amount)
orderby total descending
select new { g.Key.Code, Total = total }).ToList();
for (int i = 0; i <= 1; i++)
{
foreach (var item in qry)
{
int x = item.Code;
var result = from r in db.DownTimeCodes
where r.Code == x
select r.Description;
return result.FirstOrDefault();
}
}
}
return "Fail";
}
Now this is working OK but I can't help but think there is a simpler way of doing it. I'm also not too hot on error handling (hence there not being any). Can anyone make any recommendations on how to improve and streamline the above?