Answered by:
convert datatable column values double[]

Question
-
Hi,
My datatable has one column and 5 rows which i am binding from database.
i want to get the values from datatable and assign it to double array. is it possbile to do this uing LINQ? i don't wnat to loop the datatable and cast.
Any sample code please
loving dotnet
Answers
-
Hi,
you can use Convert.ToDouble with an appropriate CultureInfo:
var table = new DataTable("Table1"); table.Columns.Add("column1", typeof(string)); table.Rows.Add("12345"); table.Rows.Add("123.45"); table.Rows.Add("-123.45"); table.Rows.Add("1.23E3"); table.Rows.Add("4.56E-3"); double[] result = table.AsEnumerable() .Select(row => Convert.ToDouble(row.Field<string>("column1"), System.Globalization.CultureInfo.InvariantCulture)).ToArray(); Console.WriteLine(String.Join(",", result));
Regards, Elmar -
var query2 = ds.Tables[0].AsEnumerable().Select(x => Convert.ToDouble(x.Field<String>("Value"))).ToArray();
- Edited by IdahoSixString Friday, February 7, 2014 8:40 PM
- Proposed as answer by Venkat786 Friday, February 7, 2014 10:17 PM
- Marked as answer by Caillen Wednesday, February 19, 2014 6:21 AM
All replies
-
static void Main(string[] args) { DataSet ds = new DataSet(); ds.Tables.Add(new DataTable()); DataColumn dc2 = new DataColumn("Value"); dc2.DataType = System.Type.GetType("System.Double"); ds.Tables[0].Columns.Add(dc2); DataRow r1 = ds.Tables[0].NewRow(); r1["Value"] = 75.8300000000; ds.Tables[0].Rows.Add(r1); DataRow r2 = ds.Tables[0].NewRow(); r2["Value"] = 76.6800000000; ds.Tables[0].Rows.Add(r2); DataRow r3 = ds.Tables[0].NewRow(); r3["Value"] = 79.8787870000; ds.Tables[0].Rows.Add(r3); var query2 = ds.Tables[0].AsEnumerable().Select(x => x.Field<Double>("Value")); Console.ReadKey(); }
This will accomplish what you need it to do you just need to define that column as type double to avoid casting.- Proposed as answer by IdahoSixString Friday, February 7, 2014 5:30 PM
- Edited by IdahoSixString Friday, February 7, 2014 5:34 PM
-
Hi IdahoSixString,
thanks for your reply
i must be in the position to cast because from database it comes as string. so i have to cast. can you please show me the code where i can cast and convert to double array from your sample
loving dotnet
-
Hi,
you can use Convert.ToDouble with an appropriate CultureInfo:
var table = new DataTable("Table1"); table.Columns.Add("column1", typeof(string)); table.Rows.Add("12345"); table.Rows.Add("123.45"); table.Rows.Add("-123.45"); table.Rows.Add("1.23E3"); table.Rows.Add("4.56E-3"); double[] result = table.AsEnumerable() .Select(row => Convert.ToDouble(row.Field<string>("column1"), System.Globalization.CultureInfo.InvariantCulture)).ToArray(); Console.WriteLine(String.Join(",", result));
Regards, Elmar -
var query2 = ds.Tables[0].AsEnumerable().Select(x => Convert.ToDouble(x.Field<String>("Value"))).ToArray();
- Edited by IdahoSixString Friday, February 7, 2014 8:40 PM
- Proposed as answer by Venkat786 Friday, February 7, 2014 10:17 PM
- Marked as answer by Caillen Wednesday, February 19, 2014 6:21 AM