So the input of your GroupBy
is a sequence of Views
. Every View
has at least a StartDate
and SomeId
.
Your GroupBy
groups all input Views
into groups of of items extracted from the Views
with the same StartDate
. Every Group has a Key
containing this common StartDate
, the elements in the group are the SomeId
of the Views
in the group.
The result of the GroupBy
is already IQueryable<...>
. So AsQueryable
is unnecesary, it will only slow down your process.
The input of your Orderby
is a sequence of groups. Alas, groups don't have a StartDate
. Luckily, groups have a Key
containing the StartDate
that you want to order by.
var result = DbContextObject.Views
// I onlly want the views with a certain SomeId:
.Where(view => view.SomeID == CurrentlyEditingSomeID)
// group the views into groups with same StartDate
// the elements in the group are the SomeId
.GroupBy(view => view.StartDate, view=>view.SomeID)
// result: a sequence of Groups with a Key and a sequence of SomeId objects
// order the Groups by StartDate, This StartDate is in the Key
.OrderBy(group => group.Key);
By the way, if you don't want a Key
, and insist on having a StartDate
, there is a less known overload of GroupBy. A version where you can Select what you want in your output.
.GroupBy(view = view.StartDate, // make groups with same StartDate
view => view.SomeId, // select SomeId as elements in the group
(commonStartDate, someIds) => new // from every commonStartDate and the someIds
{ // in this group make a new object
StartDate = commonstartDate, // containing this common StartDate
SomeIds = someIds, // and the SomeId objects in the group
})
.OrderBy(group => group.StartDate); // now you can order by StartDate instead of Key