我想知道是否有人可以抽出几分钟给我一些建议吗?
我创建了一个 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
对列表进行分组,然后将每个“组”(稍后发生另一个循环以检查哪些成员订阅了哪些警报类别)加载到一个然后我可以将其用作电子邮件内容的变量。
最佳答案
您可以使用 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
});
如果需要,您可以选择匿名类型,并将您的序列投影到您需要的任何结构中。
关于c# - 将 IEnumerable 分组为一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31653262/