I have a list of object. The object looks like so
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; }
}
I want to use fluent LINQ to parse the the Col1
,Col2
, and Col3
properties and then sum them.
I would like to do something like this
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();
However, that float.TryParse
syntaxt in linq does not work.
How can I parse multiple columns using Fluent LINQ?