我有一个EF存储过程(函数导入),它返回一个结果集,我希望该结果集分组在多个级别上。有没有一种简单的方法可以将结果分组到一个新对象中?
我的结果集包含具有以下数据的对象:
CategoryId EventId EventInstanceId Name DueDate
我想按类别和活动对结果进行分组,因此我最终得到Category.List<Event>.List<EventInstances>
class Category
{
List<Event> Events;
}
class Event
{
List<EventInstance> Instances;
}
答案 0 :(得分:0)
好吧,我打算删除我的问题,因为我发现了,但是因为有一个向上投票,我会发布我发现的解决方案
List<CategoryModel> list =
(from category in context.GetEvents(date, date) // Stored Procedure
group category by category.CategoryId
into groupedByCategory // Group by Category
select new CategoryModel
{
CategoryId = groupedByCategory.Key,
Events = new ObservableCollection<EventModel>
(
from evt in groupedByCategory
group evt by new { evt.EventId, evt.AssignedUserId, evt.EventName, evt.EventDescription, evt.IsRecurring }
into groupedByEvent // Sub-Group for Events
select new EventModel
{
EventId = groupedByCategory.Key,
AssignedUserId = groupedByEvent.Key.AssignedUserId,
EventDescription = groupedByEvent.Key.EventDescription,
EventName = groupedByEvent.Key.EventName,
IsRecurring = groupedByEvent.Key.IsRecurring,
Instances = new ObservableCollection<EventInstanceModel>
(
from item in groupedByEvent
select new EventInstanceModel
{
CompletedDate = item.CompletedDate,
CompletedUserId = item.CompletedUserId,
DueDate = item.DueDate,
EventInstanceId = item.EventInstanceId,
IsSkipped = item.IsSkipped.GetValueOrDefault()
}
)
}
)
}
).ToList();