【问题标题】:Using linQ to group list of object into new lists of objects使用 linQ 将对象列表分组到新的对象列表中
【发布时间】:2020-01-02 01:58:21
【问题描述】:

所以我试图将ErrorItem 的列表分组到ValidationFormatErrorDTO 的列表中,具体取决于FieldTypeValidationFormat

我们的目标是拥有一个只包含一种错误类型的ValidationFormatErrorDTO 列表。

最终对象。所以我试图列出它:

  public class ValidationFormatErrorDTO
    {
       public ValidationFormat ValidationFormat { get; set; }
       public FieldType FieldType { get; set; }
       public List<ValidationFormatErrorItemDTO> ErrorItems { get; set; }
    }


   public class ValidationFormatErrorItemDTO
    {
        public int LineNumber { get; set; }
        public string LineValue { get; set; }
    }

错误对象示例:

示例 1:

 var error = new ErrorItem
 {
  LineNumber = 2,
  LineValue = "Test",
  FieldType = FieldType.DateTime,
  ValidationFormat = null
 };

示例 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();

但首先这意味着我应该对基于 FieldType 的错误执行相同的操作。这会给我 2 个 ValidationFormatErrorDTO 列表,我将不得不加入 1 个列表。

其次,使用这种方法,我遇到了IGrouping&lt;ValidationFormat, ErrorItem&gt;,我不知道该如何处理。

你们中的任何一个人都在考虑如何从我现在的位置来做这件事,或者考虑一个更好的解决方案,那太棒了!

感谢您的宝贵时间!

编辑:

所以,根据@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();

关于我现在如何合并这两者有什么想法吗? 或者如何同时按ValidationFormatFieldType 分组以获得一个列表? 像这样?

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();

【问题讨论】:

  • 最后一个 sn-p 就是你要找的。​​span>

标签: c# list linq group-by


【解决方案1】:

IGrouping我不知道怎么处理。

根据this 规范,这意味着

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

因此,从那时起,您可以获得这样的 ErrorItems 列表

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();

【讨论】:

  • 就是这样!非常感谢@Anonymous
  • 是的,这是我的荣幸:D
【解决方案2】:

因为y 是每个组,所以您可以在y 上执行.Select() 以获得List&lt;ValidationFormatErrorItemDTO&gt;

var test = 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();

【讨论】:

  • 还应按字段和格式分组,并根据键值构建 DTO。
  • @Nkosi 有什么关于我如何在同一个查询中做到这一点的例子吗?
  • 赞这个GroupBy(x =&gt; new { x.ValidationFormat, x.FieldType)}
猜你喜欢
  • 2011-02-11
  • 1970-01-01
  • 2013-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-06
相关资源
最近更新 更多