I want my users to calculate sum on their preferred field. In the code below 'var q3' is the output of a Linq query where I have queried all possible fields with grouping. These fields are Field1, Field2, ...Field10. Now in the new Linq query - "query", I want to dynamically retrieve the sum of one of the fields of my choice. This can be any of the 10 data fields in the format "Field00'. How can I do this?
If I hardcode the field name, then it works, but I want to choose a field dynamically at run time. Thus the commented out line works, but I may want to choose the other fields.
Based on some examples on the internet for related purpose, I saw a Func<> expression. However, for me it returns an error... "The type arguments for method 'System.Linq.Enumerable.Su
cannot be inferred from the usage. Try specifying the type arguments explicitly."
I am also using Dynamic Linq Queries, if that helps. Maybe I can do something like...
var query = q3.AsQueryable().Sum("Fiel
Func<string, decimal> dExpr = new Func<string, decimal>(f => Convert.ToDecimal("Field1"));
var query = from result in q3
GroupField = result.Key,
//DataField = result.Sum(f => Convert.ToDecimal(f.Field1))
DataField = result.Sum(dExpr)