none
Datatable Select convert string to decimal with invariant culture RRS feed

  • Question

  • Hi,

     

    I want to do a select statement on a datatable.

    Something like this:

    MyDataSet.MyTable.Select(string.Format("Convert(Column, System.Decimal) < {0} ",MyDecimal));

    The problem is that I need to convert my columnvalue with invariant culture, because it has '.' as decimal sign and the local culture uses ',' as decimal sign.

    This can give wrong results

    e.g.

    '3.000' will be converted to 3000 and if myDecimal has value 3,4 it will not select the row.

    How can I solve this issue?

     

     

    Thursday, December 2, 2010 11:17 AM

Answers

  • Hi Tim,

    Select method in DataTable is very limited and slow to filter the data. If you're not limited to .NET Framework 2.0, use LINQ (or Lambda Expressions) to select correct data.

    Below is an example:

    // LINQ
    var filteredData = from column in dataSet1.DataTable1
              where column.DecimalData == 0.12m
              select column;
    
    // Lambda Expression
    var filteredData = dataSet1.DataTable1.Where(column => column.DecimalData == 0.12m);

    Regards,
    Danijel

    • Marked as answer by ---Eddie--- Friday, December 3, 2010 2:07 PM
    Thursday, December 2, 2010 11:45 PM

All replies

  • Hi Tim,

    Select method in DataTable is very limited and slow to filter the data. If you're not limited to .NET Framework 2.0, use LINQ (or Lambda Expressions) to select correct data.

    Below is an example:

    // LINQ
    var filteredData = from column in dataSet1.DataTable1
              where column.DecimalData == 0.12m
              select column;
    
    // Lambda Expression
    var filteredData = dataSet1.DataTable1.Where(column => column.DecimalData == 0.12m);

    Regards,
    Danijel

    • Marked as answer by ---Eddie--- Friday, December 3, 2010 2:07 PM
    Thursday, December 2, 2010 11:45 PM
  • Ok Thanks,

     

    I have no experience using LINQ but the above statement says it all.

     

    My statement now looks something like this:

     

    MyDataSet.MyTable.Where( table => double.Parse(table.column, InvariantCulture) < MyDecimal);

     

    And it works fine. Now my stringvalue is converted using invariantculture and my code looks a whole lot less messy :)

    Friday, December 3, 2010 2:11 PM
  • Tim,

    Why don't you use a Decimal column instead of a String column? In this case you don't need to convert data in InvariantCulture. That's just a tip.

    To learn more about LINQ, you can start here:

    ...or with more general - http://msdn.microsoft.com/en-us/netframework/aa904594.aspx


    Regards,
    Danijel

    Blog, Twitter
    Friday, December 3, 2010 4:37 PM
  • Because, the column can contain decimals and strings.

    There is another column that keeps track of the datatype the column has and depending on that type I need to parse the column to string, decimal, date ...

    Otherwise I would have used a decimal column.

    Monday, December 6, 2010 7:35 AM