[英]Creating Nested Groupings using Linq Extension Methods
I am building an application that has a diary feature, and within that object there are a list of appointments. 我正在构建一个具有日记功能的应用程序,并且在该对象中有一个约会列表。 For display purposes I have a requirement to create a nested groupings in the following format
出于显示目的,我需要以以下格式创建嵌套分组
Heading 1
Sub-heading 1
Data row 1
Data row 2
Sub-heading 2
Data row 1
Data row 2
Heading 2
Sub-heading 1
Data row 1
Data row 2
where heading is the office that the appointment is in, and the sub-heading is the room that this is in. This will make more sense when looking at the following models; 标题是约会所在的办公室,子标题是约会所在的房间。当查看以下模型时,这将更有意义;
public class Diary
{
public int Id {get; set; }
public DateTime DiaryDate {get; set; }
List<Appointment> Appointments {get; set;}
}
public class Appointment
{
public int Id {get; set;}
public int DiaryId {get; set;}
public DateTime StartTime {get; set;}
public DateTime EndTime {get; set; }
public string Attendees {get; set; }
public Office Office {get; set; }
public Room Room {get; set; }
}
Office
is an enum with office locations in, as is Room
with rooms in it. Office
是一个在办公室位置的枚举, Room
包含房间。
Currently I have solved this problem using this code: 目前,我已使用以下代码解决了此问题:
Appointments.GroupBy(k => k.Office, k => k)
.ToDictionary(k => k.Key,
k => k.ToList().GroupBy(sk => sk.Room)
.ToDictionary(mk => mk.Key, mk => mk.ToList()));
I am writing the diary to a view model where the appointments is of type 我正在将日记写到约会类型为的视图模型
public Dictionary<Office, Dictionary<Room, List<Appointment>>> Appointments { get; set; }
Essentially, my long group by statement is grouping the the data by Office
and then using the resultSelector
overload to project the original object. 本质上,我的长group by语句是按
Office
对数据进行分组,然后使用resultSelector
重载来投影原始对象。 Then, I am turning that IGrouping
result into a Dictionary
, where the key is office and value is list of type Appointment
. 然后,我将
IGrouping
结果转换为Dictionary
,其中的键是office,值是Appointment
类型的列表。 From that I am then grouping each list of type Appointment
by Room
, creating a dictionary within a dictionary, which produces the type Dictionary
with Key Office
, and then dictionary with key Room
and list of Appointment
as the value to each key. 然后,我
Appointment
Room
类型划分的每个Appointment
列表分组,在字典中创建一个字典,该字典生成带有Key Office
Dictionary
类型,然后生成具有Room
和Appointment
列表作为每个键值的字典。
This code produces the desired result, but I think it is difficult to read, more difficult to understand in the view when looping and probably quite inefficient. 这段代码产生了预期的结果,但是我认为它很难阅读,在循环时在视图中更难理解,并且可能效率很低。
Can somebody offer some advice as how I can simply the way I achieve my desired result? 有人可以提供一些建议吗,因为我怎样才能简单地达到预期效果?
You're actually building up a lookup, a dictionary with multiple values per key. 您实际上是在建立一个查找,即每个键具有多个值的字典。 Just use that.
只是使用那个。
Your query simply becomes: 您的查询将变成:
var query = appointments.OrderBy(a => a.Office).ThenBy(a => a.Room)
.GroupBy(a => a.Office)
.ToDictionary(g => g.Key, g => g.ToLookup(a => a.Room));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.