将 IEnumerable 分组为一个字符串
Group IEnumerable into a string
我想知道是否有人可以抽出几分钟时间给我一些建议?
我创建了一个 IEnumerable
列表:
public class EmailBlock
{
public int alertCategory { get; set; }
public string alertName { get; set; }
public string alertURL { get; set; }
public string alertSnippet { get; set; } //Need to work out the snippet
}
List<EmailBlock> myEmailData = new List<EmailBlock>();
然后我循环遍历一些数据(Umbraco 内容 - 不是真正相关的!)并将项目添加到列表中。
myEmailData.Add(new EmailBlock { alertCategory = category.Id, alertName = alert.GetPropertyValue("pageTitle"), alertURL = alert.NiceUrl });
最终我想做的是按 alertCategory
对列表进行分组,然后将每个 'group' 加载到(稍后发生另一个循环以检查哪些成员订阅了哪些警报类别)到一个变量,然后我可以将其用作电子邮件的内容。
您可以使用 Linq 的 GroupBy()
来执行此操作:
using System.Linq
...
//Create a type to hold your grouped emails
public class GroupedEmail
{
public int AlertCategory { get; set; }
public IEnumerable<EmailBlock> EmailsInGroup {get; set; }
}
var grouped = myEmailData
.GroupBy(e => e.alertCategory)
.Select(g => new GroupedEmail
{
AlertCategory = g.Key,
EmailsInGroup = g
});
如果需要,您可以 select 匿名类型并将您的序列投影到您需要的任何结构中。
Linq 有一个很好的 group by 语句:
var emailGroup = emailList.GroupBy(e => e.alertCategory);
然后你可以遍历每个分组并做任何你想做的事:
foreach(var grouping in emailGroup)
{
//do whatever you want here.
//note grouping will access the list of grouped items, grouping.Key will show the grouped by field
}
编辑:
要在分组后检索一组,只需使用 Where
表示多个或 First
表示一个:
var group = emailGroup.First(g => g.Key == "name you are looking for");
或
var groups = emailGroup.Where(g => listOfWantedKeys.Contains(g.Key));
这比每次需要查找时都循环遍历要高效得多。
我想知道是否有人可以抽出几分钟时间给我一些建议?
我创建了一个 IEnumerable
列表:
public class EmailBlock
{
public int alertCategory { get; set; }
public string alertName { get; set; }
public string alertURL { get; set; }
public string alertSnippet { get; set; } //Need to work out the snippet
}
List<EmailBlock> myEmailData = new List<EmailBlock>();
然后我循环遍历一些数据(Umbraco 内容 - 不是真正相关的!)并将项目添加到列表中。
myEmailData.Add(new EmailBlock { alertCategory = category.Id, alertName = alert.GetPropertyValue("pageTitle"), alertURL = alert.NiceUrl });
最终我想做的是按 alertCategory
对列表进行分组,然后将每个 'group' 加载到(稍后发生另一个循环以检查哪些成员订阅了哪些警报类别)到一个变量,然后我可以将其用作电子邮件的内容。
您可以使用 Linq 的 GroupBy()
来执行此操作:
using System.Linq
...
//Create a type to hold your grouped emails
public class GroupedEmail
{
public int AlertCategory { get; set; }
public IEnumerable<EmailBlock> EmailsInGroup {get; set; }
}
var grouped = myEmailData
.GroupBy(e => e.alertCategory)
.Select(g => new GroupedEmail
{
AlertCategory = g.Key,
EmailsInGroup = g
});
如果需要,您可以 select 匿名类型并将您的序列投影到您需要的任何结构中。
Linq 有一个很好的 group by 语句:
var emailGroup = emailList.GroupBy(e => e.alertCategory);
然后你可以遍历每个分组并做任何你想做的事:
foreach(var grouping in emailGroup)
{
//do whatever you want here.
//note grouping will access the list of grouped items, grouping.Key will show the grouped by field
}
编辑:
要在分组后检索一组,只需使用 Where
表示多个或 First
表示一个:
var group = emailGroup.First(g => g.Key == "name you are looking for");
或
var groups = emailGroup.Where(g => listOfWantedKeys.Contains(g.Key));
这比每次需要查找时都循环遍历要高效得多。