所以你的输入GroupBy
是一个序列Views
。每个View
至少有一个StartDate
和SomeId
。
您的GroupBy
组都输入到从相同Views
的 中提取的项目组中。每个 Group 都有一个包含这个 common的,组中的元素是组中的。Views
StartDate
Key
StartDate
SomeId
Views
的结果GroupBy
已经是IQueryable<...>
。所以AsQueryable
是不必要的,它只会减慢你的进程。
您的输入Orderby
是一组组。唉,团体没有StartDate
. 幸运的是,组有一个Key
包含StartDate
您想要订购的。
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);
顺便说一句,如果您不想要 aKey
并坚持拥有 a StartDate
,则GroupBy 的重载则鲜为人知。一个版本,您可以在其中选择您想要的输出。
.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