Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Consider I have data table with two columns,

C#
1. ItemID --> String data type
2. Items --> Decimal data type


I want to find Total Items Per ItemID. But my column and data type may change. So please
give suggestions on following syntax

C#
var query = from row in dt.AsEnumerable()
                                     group row by row.Field<String>(0) into grp
                                     select new
                                     {
                                         ItemID = grp.Key,
                                         TotalItems = grp.Sum(r => r.Field<Decimal>(2))
                                     };


In above syntax, instead of passing <String>, I want to pass data type dynamically depending on datatype of column.
Posted
Updated 7-Mar-14 3:16am
v2

1 solution

use dynamic type.

C#
var query = from row in dt.AsEnumerable()
                                     group row by row.Field<dynamic>(0) into grp
                                     select new
                                     {
                                         ItemID = grp.Key,
                                         TotalItems = grp.Sum(r => r.Field<Decimal>(2))
                                     };


Dynmic type is bypass the data type checking so you case dynamically get the results from the above query.

Ref. : Using Type dynamic[^]
 
Share this answer
 
Comments
Maciej Los 7-Mar-14 13:15pm    
+5!
Member 4373780 11-Mar-14 9:08am    
You are right but I want to store those values using following,
foreach (var grp in query)
{
DataRow dr = dtMainChartData.NewRow();
dr["KeyColumn"] = grp.KeyColumn;
dr["Total"] = grp.Total;
dtMainChartData.Rows.Add(dr);
}

Here how can I set data type of data type of data column as dynamic. Here I have set data type of data column accordingly Data type of SQL in runtime like,

KeyColumnDataType = dtDrillDownChartData.Columns[0].DataType.FullName;
TotalDataType = dtDrillDownChartData.Columns[2].DataType.FullName;

dtMainChartData.Columns.Add("KeyColumn", System.Type.GetType(KeyColumnDataType));
dtMainChartData.Columns.Add("Total", System.Type.GetType(TotalDataType));
Anand Gunasekaran 11-Mar-14 13:59pm    
Hi, I changed the mentioned code that you have posted. Here you check the code.

foreach (var grp in query)
{
DataRow dr = dtMainChartData.NewRow();
dr["KeyColumn"] = grp.ItemID[0];
dr["Total"] = grp.ItemID[2];
dtMainChartData.Rows.Add(dr);
}

dtMainChartData.Columns.Add("KeyColumn", System.Type.GetType(dtDrillDownChartData.Columns[0].DataType.FullName));
dtMainChartData.Columns.Add("Total", System.Type.GetType(dtDrillDownChartData.Columns[2].DataType.FullName));

Thanks & Regards,
Anand. G

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900