[英]LINQ - How to concatenate two fields in a Group By?
我有一个 ReportStatusEntity class,如下所示:
public class ReportsStatusEntity
{
public string PolicyNumber { get; set; }
public string ClientName { get; set; }
public bool HasIndividualBrokers { get; set; }
}
假设我有以下List<ReportStatusEntity>()
:
{PolicyNumber = 1, ClientName = "John Doe", HasIndividualBrokers = True},
{PolicyNumber = 1, ClientName = "Sarah Doe", HasIndividualBrokers = True},
{PolicyNumber = 2, ClientName = "Paul Smith", HasIndividualBrokers = False},
{PolicyNumber = 3, ClientName = "Ryan Johnson", HasIndividualBrokers = False}
我想按 PolicyNumber 分组,然后将具有相同 PolicyNumber 的 ClientNames 与“&”连接起来。
分组应该是这样的:
{PolicyNumber = 1, ReportStatusEntity = (PolicyNumber = 1, ClientName = "John Doe & Sarah Doe", HasIndividualBrokers = True)},
{PolicyNumber = 2, ReportStatusEntity = (PolicyNumber = 2, ClientName = "Paul Smith", HasIndividualBrokers = False)},
{PolicyNumber = 3, ReportStatusEntity = (PolicyNumber = 3, ClientName = "Ryan Johnson", HasIndividualBrokers = False)}
如何使用 LINQ 在 C# 中完成此操作? 谢谢你。
var list = new List<ReportsStatusEntity>()
{
new ReportsStatusEntity{PolicyNumber = "1", ClientName = "John Doe", HasIndividualBrokers = true},
new ReportsStatusEntity{PolicyNumber = "1", ClientName = "Sarah Doe", HasIndividualBrokers = true},
new ReportsStatusEntity{PolicyNumber = "2", ClientName = "Paul Smith", HasIndividualBrokers = false},
new ReportsStatusEntity{PolicyNumber = "3", ClientName = "Ryan Johnson", HasIndividualBrokers = false}
};
var results = list.GroupBy(r => r.PolicyNumber)
.Select(g => new
{
PolicyNumber = g.Key,
// string.Join will not work if its against a database with sql
ClientNames = string.Join(" & ", g.Select(r => r.ClientName)),
});
foreach (var result in results)
{
Console.WriteLine($"Policy {result.PolicyNumber}: {result.ClientNames}");
}
// -- Outputs --
// Policy 1: John Doe & Sarah Doe
// Policy 2: Paul Smith
// Policy 3: Ryan Johnson
您可以先按 PolicyNumber 对所有 ReportStatusEntity 进行分组
List<List<ReportStatusEntity>> listReportStatusEntityGroupped = this.yourList.GroupBy(u => new { u.PolicyNumber,u.HasIndividualBrokers })
.Select(grp => grp.ToList())
.ToList();
new { u.PolicyNumber }
不是强制性的,因为您只对一个参数进行分组,但您可以添加其他参数。
然后,一旦你有了List<List<ReportStatusEntity>>
,你就可以创建一个 foreach:
List<ReportStatusEntity> outputList=new List<ReportStatusEntity>();
foreach(List<ReportStatusEntity> listReportInGroup in listReportStatusEntityGroupped)
{
ReportStatusEntity newReport = new ReportStatusEntity
{
PolicyNumber=listReportInGroup[0].PolicyNumber,
HasIndividualBrokers = listReportInGroup[0].HasIndividualBrokers,
ClientName = string.Join(" & ",listRepInGroup.Select(x=>x.ClientName)),
};
outputList.Add(newReport);
}
1- 创建一个 class 如下:
public class ReportsStatusLastEntity
{
public int Id { get; set; }
public string PolicyNumber { get; set; }
public string ClientName { get; set; }
public bool HasIndividualBrokers { get; set; }
}
2- 创建List <ReportsStatusLastEntity> liste= new List<ReportsStatusLastEntity>()
3- 通过列表 ReportStatusEntity 启动 foreach 循环
4- 检查 ReportsStatusLastEntity Id 上的 PolicyNumber。 如果为真,请添加名称。 否则添加新项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.