Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends

How to create a dictionary with data table column name as key and value as row values.

My dictionary declaration is
C#
Dictionary<string, double> prereturnValues = new Dictionary<string, double>();


Please help

What I have tried:

C#
dtStratergic.AsEnumerable().ToDictionary<DataRow, string, double>(row => row.Field<string>(0), row => row.Field<double>(rowPosition)
Posted
Updated 5-May-16 19:11pm
v2
Comments
Karthik_Mahalingam 5-May-16 5:27am    
how do you want the row values ?
give some example
jinesh sam 5-May-16 6:09am    
i need to fetch only particular row by row index (rowPosition).
dictionary looks like
AE, .2535
GH, .568

try this
C#
DataTable dt = new DataTable();
         dt.Columns.Add("DoubleColumn1");
         dt.Columns.Add("DoubleColumn2");
         dt.Rows.Add(1, 2);
         dt.Rows.Add(2, 4);

         Dictionary<string, double[]> dict = new Dictionary<string, double[]>();
         for (int i = 0; i < dt.Columns.Count; i++)
             dict.Add(dt.Columns[i].ColumnName, dt.Rows.Cast<DataRow>().Select(k => Convert.ToDouble(k[dt.Columns[i]])).ToArray());


in lambda:
C#
dtStratergic.Columns.Cast<DataColumn>().ToList().ForEach( c=>  prereturnValues.Add(c.ColumnName,Convert.ToDouble(WSG_Planning.dtStratergic.Rows[rowPosition][c.ColumnName].ToString())));
 
Share this answer
 
v2
Comments
jinesh sam 5-May-16 6:12am    
Thanks for posting the answer. but my requirement is slightly different. No need to of double[] array
Karthik_Mahalingam 5-May-16 6:26am    
then?
if the table has multiple rows?
jinesh sam 5-May-16 14:56pm    
I need to pick only one row based on the row index positions.
Karthik_Mahalingam 6-May-16 0:00am    
can you give a sample of input and output data, so that it will be easy to understand your need.
jinesh sam 6-May-16 1:12am    
I got answer... Can u please convert my solution to Lambda expression
I got a solution for my Question.

C#
foreach (DataColumn column in dtStratergic.Columns)
{  prereturnValues.Add(column.ColumnName,Convert.ToDouble(WSG_Planning.dtStratergic.Rows[rowPosition][column.ColumnName].ToString()));
}


Can any one please provide Lambda expression for this
 
Share this answer
 

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