是否有一种简单的方法将EF ResultSet分组到一个新对象中
本文关键字:一个 对象 新对象 一种 简单 EF 是否 方法 ResultSet | 更新日期: 2023-09-27 18:08:53
我有一个返回结果集的EF存储过程(函数导入),并且我希望该结果集在多个级别上分组。是否有一种简单的方法将结果分组到一个新对象中?
我的结果集包含以下数据的对象:
<>之前被标记EventIdEventInstanceId名字DueDate之前我想按类别和事件对结果进行分组,所以我最终得到Category.List<Event>.List<EventInstances>
class Category
{
List<Event> Events;
}
class Event
{
List<EventInstance> Instances;
}
既然我想出来了,我本来打算删掉我的问题,但既然有一个向上的投票,我就把我找到的解决方案贴出来
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();