![](/img/trans.png)
[英]Using Linq to group a list of objects into a new grouped list of list of objects
[英]Using linQ to group list of object into new lists of objects
所以我试图根据FieldType
或ValidationFormat
将ErrorItem
列表ErrorItem
到ValidationFormatErrorDTO
列表中。
目标是让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();
关于我现在如何合并这两个的任何想法? 或者如何同时按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()
以获得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.