I have a model similar to this:
Public class Item {
public int Id { get; set; }
public string Name { get; set; }
public DateTime? StartTime { get; set; }
public DateTime? EndTime { get; set; }
public int? TotalTime
{
get
{
return Convert.ToInt32(EndTime.Value - StartTime.Value).TotalMinutes);
}
}
etc...
Then I get a list of Items:
IQueryable<Items> items = itemsContext.Items.Where(e => e.Id > 0);
I do some calculations, run some other code, then at the end I want to return the list of Items ordered by Name ASC and TotalTime DESC. This is what I tried:
items = items.OrderBy(e => e.Name).ThenByDescending(e => e.TotalTime);
But I get an error: "The LINQ expression could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync()."
I also tried this, but get the same error:
items = items.OrderBy(e => e.Name).ThenByDescending(e => e.TotalTime ?? 0);
How can I sort my data set by TotalTime?