C# 使用linQ将对象列表分组到新的对象列表中

C# 使用linQ将对象列表分组到新的对象列表中,c#,list,linq,group-by,C#,List,Linq,Group By,因此,根据字段类型或验证格式,我尝试将ErrorItem列表分组为ValidationFormatErrorDTO 目标是使ValidationFormatErrorDTO列表只包含一种类型的错误 最后一个目标。所以我试着列出它: 例2: error = new ErrorItem { LineNumber = 11, LineValue = "john.doe.test.com", ValidationFormat = ValidationFormat

因此,根据
字段类型
验证格式
,我尝试将
ErrorItem
列表分组为
ValidationFormatErrorDTO

目标是使
ValidationFormatErrorDTO
列表只包含一种类型的错误

最后一个目标。所以我试着列出它: 例2:

   error = new ErrorItem
   {
     LineNumber = 11,
     LineValue = "john.doe.test.com",
     ValidationFormat = ValidationFormat.Email,
     FieldType = null
    };
例3:

   error = new ErrorItem
   {
     LineNumber = 32,
     LineValue = "1212",
     ValidationFormat = ValidationFormat.PhoneNumber,
     FieldType = null
    };
从这里我被困住了

我试过这个:

var test = tempListErrors.GroupBy(x => x.ValidationFormat)
                    .Select(y => new ValidationFormatErrorDTO
                    {
                        ValidationFormat = y.Key,
                        ErrorItems = ??
                    }).ToList();
但首先,这意味着我应该对基于字段类型的错误执行相同的操作。Wich会给我两个
ValidationFormatErrorDTO
列表,我必须加入到一个列表中

第二,使用这种方法,我陷入了一种我不知道如何处理的
I分组

我想你们中的任何人都可以从我现在的位置考虑如何使用它,或者考虑一个更好的解决方案,这将是非常棒的

谢谢你的时间

编辑: 因此,根据@Ashkan的回答,我会这样:

var formatErrors = tempListErrors.GroupBy(x => x.ValidationFormat)
                    .Select(y => new ValidationFormatErrorDTO
                    {
                        ValidationFormat = y.Key,
                        ErrorItems = y.Select(x => new ValidationFormatErrorItemDTO
                        {
                            LineNumber = x.LineNumber,
                            LineValue = x.LineValue
                        }).ToList()
                    }).ToList();

                var fieldTypeErrors = tempListErrors.GroupBy(x => x.FieldType)
                    .Select(y => new ValidationFormatErrorDTO
                    {
                        FieldType = y.Key,
                        ErrorItems = y.Select(x => new ValidationFormatErrorItemDTO
                        {
                            LineNumber = x.LineNumber,
                            LineValue = x.LineValue
                        }).ToList()
                    }).ToList();
你知道我现在如何合并这两个吗? 或者如何同时按
ValidationFormat
FieldType
进行分组,从而只获得一个列表? 像这样的

var both = tempListErrors.GroupBy(x => new { x.ValidationFormat, x.FieldType })
                    .Select(y => new ValidationFormatErrorDTO
                    {
                        ValidationFormat = y.Key.ValidationFormat,
                        FieldType = y.Key.FieldType,
                        ErrorItems = y.Select(x => new ValidationFormatErrorItemDTO
                        {
                            LineNumber = x.LineNumber,
                            LineValue = x.LineValue
                        }).ToList()
                    }).ToList();

由于
y
是每个组的最大值,因此您可以在
y
上执行
.Select()
以获得
列表


由于
y
是每个组的最大值,因此您可以在
y
上执行
.Select()
以获得
列表

我不知道如何处理我的分组

根据规范,这意味着

表示具有公用键的对象的集合

因此,从这一点上,你可以得到一个像这样的错误项列表

ErrorItems = group.Select(item => new ValidationFormatErrorItemDTO 
                          {
                              LineNumber = item.LineNumber,
                              LineValue = item.LineValue
                          }).ToList()
已更新

var test = tempListErrors.GroupBy(p => new { p.ValidationFormat, p.FieldType})
                    .Select(group => new ValidationFormatErrorDTO
                    {
                        ValidationFormat = group.Key.ValidationFormat,
                        ErrorItems = group.Select(item => new ValidationFormatErrorItemDTO 
                          {
                              LineNumber = item.LineNumber,
                              LineValue = item.LineValue
                          }).ToList()
                    }).ToList();
我不知道如何处理我的分组

根据规范,这意味着

表示具有公用键的对象的集合

因此,从这一点上,你可以得到一个像这样的错误项列表

ErrorItems = group.Select(item => new ValidationFormatErrorItemDTO 
                          {
                              LineNumber = item.LineNumber,
                              LineValue = item.LineValue
                          }).ToList()
已更新

var test = tempListErrors.GroupBy(p => new { p.ValidationFormat, p.FieldType})
                    .Select(group => new ValidationFormatErrorDTO
                    {
                        ValidationFormat = group.Key.ValidationFormat,
                        ErrorItems = group.Select(item => new ValidationFormatErrorItemDTO 
                          {
                              LineNumber = item.LineNumber,
                              LineValue = item.LineValue
                          }).ToList()
                    }).ToList();

还应按字段和格式分组,并根据键值构建DTO。@Nkosi有没有关于如何在同一查询中执行此操作的示例?如
GroupBy(x=>new{x.ValidationFormat,x.FieldType)}
还应根据字段和格式进行分组,并根据键值构建DTO。@有没有关于如何在同一查询中实现这一点的示例?就像这样
GroupBy(x=>new{x.ValidationFormat,x.FieldType)}
最后一个代码段就是您要查找的。最后一个代码段就是您要查找的。就是它!非常感谢@匿名是的,这是我的荣幸:就是这样!非常感谢@匿名是的,这是我的荣幸:D
var test = tempListErrors.GroupBy(p => new { p.ValidationFormat, p.FieldType})
                    .Select(group => new ValidationFormatErrorDTO
                    {
                        ValidationFormat = group.Key.ValidationFormat,
                        ErrorItems = group.Select(item => new ValidationFormatErrorItemDTO 
                          {
                              LineNumber = item.LineNumber,
                              LineValue = item.LineValue
                          }).ToList()
                    }).ToList();