繁体   English   中英

使用 linQ 将对象列表分组为新的对象列表

[英]Using linQ to group list of object into new lists of objects

所以我试图根据FieldTypeValidationFormatErrorItem列表ErrorItemValidationFormatErrorDTO列表中。

目标是让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 的错误做同样的事情。 Wich 会给我 2 个ValidationFormatErrorDTO列表,我必须加入 1 个列表。

其次,使用这种方法我被困在IGrouping<ValidationFormat, ErrorItem>我不知道如何处理。

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

谢谢你的时间 !

编辑:

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

由于y是每个组,因此您可以对y执行.Select()以获得List<ValidationFormatErrorItemDTO>

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

IGrouping 不知道怎么处理。

根据这个规格,这意味着

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM