合并列表中的行。如果一列有不同的值

时间:2015-05-13 12:43:26

标签: c# linq

我使用以下逻辑将列表中的List行合并在一起,如果它们除了一个(OtherAuditeesUserName)之外都具有相同的列。在这种情况下,我使用逗号分隔符

加入OtherAuditeesUserName的不同值

原始列表如下所示:

TrailRemarkId,PreviousAudit,OtherAuditees,StrategicPriority,观测,DocumentsReviewed

1,“old audit”,“jane smith”,1,none,doc.docx

1,“old audit”,“john collins”,1,none,doc.docx

我要找的最终结果是:

TrailRemarkId,PreviousAudit,OtherAuditees,StrategicPriority,观测,DocumentsReviewed

1,“老审计”,“jane smaith,john collins”,1,none,doc.docx

了解如何使用逗号加入OtherAuditees。

有人能指出合并List行的更有效方法吗?

 var trailRemarks = (from a in auditData
                                select new
                                {
                                    a.TrailRemarkId,
                                    a.PreviousAudit,
                                    a.OtherAuditees,
                                    a.StrategicPriority,
                                    a.Observations,
                                    a.DocumentsReviewed,
                                }).Distinct();

 List<TrailRemarkEntity> trlist = new List<TrailRemarkEntity>() ;
            int? trId = 0;
            foreach (var tr in trailRemarks)
            {
                if (trId == 0 || (trId != tr.TrailRemarkId))
                {
                    trlist.Add(
                        new TrailRemarkEntity()
                        {
                            TrailRemarkId = tr.TrailRemarkId ?? 0,
                            PreviousAuditName = tr.PreviousAudit,
                            DocumentsReviewed = tr.DocumentsReviewed,
                            StrategicPriorityName = tr.StrategicPriority,
                            OtherAuditeesUserName = tr.OtherAuditees,
                            Observations = tr.Observations
                        }
                        );
                }
                else
                {
                    var existingTR = trlist.Last();
                    existingTR.OtherAuditeesUserName += ", " + tr.OtherAuditees;
                }

                trId = tr.TrailRemarkId;

            }

2 个答案:

答案 0 :(得分:1)

您可以使用group by和string.join执行此操作,如下所示

List<TrailRemarkEntity> trailRemarks = (from a in auditData
                            group a by new {
                                a.TrailRemarkId,
                                a.TrailRemarkId,
                                a.PreviousAudit,
                                a.StrategicPriority,
                                a.Observations,
                                a.DocumentsReviewed
                            } into groupedData

                            select new TrailRemarkEntity()
                            {
                                TrailRemarkId = groupedData.Key.TrailRemarkId ?? 0,
                                PreviousAuditName = groupedData.Key.PreviousAudit,
                                DocumentsReviewed = groupedData.Key.DocumentsReviewed,
                                StrategicPriorityName = groupedData.Key.StrategicPriority,
                                OtherAuditeesUserName = string.join("," , groupedData.Select(exp=>exp.OtherAuditees)),
                                Observations = groupedData.Key.Observations
                            }).ToList();

希望它有所帮助。

答案 1 :(得分:0)

感谢thisiva,您的解决方案有效但我稍微修改了一下以删除重复项,如下所示:

string.Join(“,”,groupedData.Select(exp =&gt; exp.OtherAuditees).Distinct())