【发布时间】:2018-01-23 05:11:58
【问题描述】:
我有一个对象列表。对象是这样的
public class GenericRecord
{
public string GroupName { get; set; }
public DateTime CreatedAt { get; set; }
public string Col1 { get; set; }
public string Col2 { get; set; }
public string Col3 { get; set; }
public float ConvertedCol1 { get; set; }
public float ConvertedCol3 { get; set; }
public float ConvertedCol3 { get; set; }
}
我想使用 fluent LINQ 解析 Col1、Col2 和 Col3 属性,然后对它们求和。
我想做这样的事情
IEnumerable<GenericRecord> records = ...;
var summaries = records.Select(x => new GenericRecord
{
float.TryParse(x.Col1, out ConvertedCol1),
float.TryParse(x.Col2, out ConvertedCol2),
float.TryParse(x.Col3, out ConvertedCol3),
GroupName => x.GroupName
}).GroupBy(x => x.GroupName)
.Select(x => new {
GroupName = x.Key,
Total1 = x.Sum(y => y.ConvertedCol1),
Total2 = x.Sum(y => y.ConvertedCol2),
Total3 = x.Sum(y => y.ConvertedCol3),
Count = x.Count()
}).ToList();
但是,linq 中的 float.TryParse 语法不起作用。
如何使用 Fluent LINQ 解析多个列?
【问题讨论】: