You have the correct syntax (my test fields are "One" and "Two")
new ( Key.One, Sum(it["Two"]) as Two )
However there is no linq method Sum(object), you need to tell it what it is Summing (int, float, decimal etc)
The easiest way is to change the dictionary definition to Dictionary<'string, int> assuming you are summing integers.
I faced this issue myself; the problem is that
Sum() is seeing the column as type
object (which can be applied to
Max() even, but not
Sum()), and thus is failing; note that it says there is no applicable aggregate function.
The solution is to use an inline conversion to integer. Dynamic LINQ supports this conversion, and can be done as follows in your example:
selectSB.Append("Sum(") .Append("Convert.ToInt32(") // Add this... .Append("it[\"") .Append(name) .Append("\"]") .Append(")") // ... and this .Append(") as ") .Append(name);
If your columns aren't
int type, I believe
bigint) works, as well as
- C# Dynamic LINQ: Select with Sum on dictionary indexer
- C# Create object with dynamic properties : LINQ select List<object> values by property names array
- How do I define a SELECT TOP using LINQ with a dynamic query?
- Dynamic LINQ - Struggling with GROUP BY / SELECT Syntax
- How to create dynamic Linq Select Expression with anonymous objects
- Handle a Dynamic Select With Dyanmic Linq
- Dynamic LINQ expression for Select with child collection (Entity Framework)
- Linq get all fields from select along with dynamic column
- Create dynamic LINQ expression for Select with FirstOrDefault inside
- Dynamic select in query with linq
- Select parent but order by certain child with dynamic linq
- Dynamic Linq Query with multiple select value's
- How can I dynamically select my Table at runtime with Dynamic LINQ
- Dynamic Linq Select with Lambda
- Help with Linq and dynamic object property in select
- Replacing conditional operator in the linq select with dynamic property
- Entity Framework dynamic linq where from generic source with dynamic where clause
- Dynamic Linq with dictionary keys
- Dynamic LINQ - Groupby with ExpandoObject Results in Error: No Applicable Indexer
- Dynamic select sentence with LINQ - Join 2 DataTables
- How to use LINQ to select object with minimum or maximum property value
- Select a Dictionary<T1, T2> with LINQ
- LINQ Select Distinct with Anonymous Types
- Select multiple records based on list of Id's with linq
- How to select only the records with the highest date in LINQ
- LINQ - Convert List to Dictionary with Value as List
- Linq code to select one item
- How to use LINQ with dynamic collections
- LINQ : Dynamic select
- Flatten a C# Dictionary of Lists with Linq
- Getting a casting error for no visible reason
- DataTable linq query iteration
- The entity or complex type 'CreditFacility_Web.Models.CreditFacilityModel.SavingsAccount' cannot be constructed in a LINQ to Entities query
- Sorting List<TextBox> Numerically C#
- Sql to LINQ 'where in' query
- LINQ to SQL select property name by string on projection
- Inserting selected fields into a ToList
- How to convert two columns into a single string column with dynamic linq?
- LINQ to SQL to find values starting with characters from another table
- Multi-level grouping in LINQ?
- Is sorting in LINQ by Ascii code?
- How to create a left join instead of inner join?
- what's the best return type for Select LINQ
- Group and sort elements in an XML document with LINQ in C#
- Is there a way to create a list of property values from a list of some type?
- Due to LINQ Retrieving of Record data's i have this error: NullReferenceException was unhandled by user code
- Dynamically building queries
- How to view LINQ to SQL Query results in Messagebox?
- Search in dictionary of dictionaries
- How can I use this Linq Query using it with a multiple parameter by Join tables