LINQ -如何在Group By中连接两个字段?



我有一个ReportStatusEntity类,如下所示:

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)}

如何在c#中使用LINQ完成?谢谢你。

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-按如下方式创建一个类:

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-启动foreach循环列表ReportStatusEntity

4-查看ReportsStatusLastEntity Id上的PolicyNumber。如果为真,添加名称。

最新更新