locked
Unable to cast object of type 'System.Double' to type 'System.String'. RRS feed

  • Question

  • Hi,

    when i executing this query getting problem ;

    Problem is :-   Unable to cast object of type 'System.Double' to type 'System.String'.

     var newData = from c in dtnew.AsEnumerable().Where(x => x.Field<string>(cbColumnData.SelectedItem.ToString()) != "" && x.Field<string>(cbColumnData.SelectedItem.ToString()) != "-")
                                      group c by c.Field<string>(cbColumnData.SelectedItem.ToString()) into g
                                      orderby g.ToString()
                                      select new { SelectedColumnName = Convert.ToString(g.Key), DistinctCount =Convert.ToString( g.Count()) };


    Please mark the post as answer if it is helpfull to you

    Thursday, November 21, 2013 7:01 PM

Answers

  • ok so first thing is first please next time use code blocks.

    Anyhow your problem is not that you are not casting to string its when you use

    x.Field<string>

    This is a contractual obligation to be this data type so go back and make sure your table or database is the same datatype in which you are saying it is.

    So without knowing exactly which contract is failing I would make sure that you have the correct data types for each of these locations.

    NOTE Areas to check data contract are surrounded with ##.

     var newData = from c in dtnew.AsEnumerable().Where(x => #x.Field<string>#(cbColumnData.SelectedItem.ToString()) != "" && #x.Field<string>#(cbColumnData.SelectedItem.ToString()) != "-")
                                       group c by #c.Field<string>#(cbColumnData.SelectedItem.ToString()) into g
                                       orderby g.ToString()
                                       select new { SelectedColumnName = Convert.ToString(g.Key), DistinctCount =Convert.ToString( g.Count()) };

    So you will want to change those sections to something similar to this without knowing your data types.

    x.Field<double>

    c.Field<double>

    If you are unsure of the datatype you are pulling you can always just say it is object.

    x.Field<object>

    c.Field<object>

    Doing this will add a lot of overhead of boxing and unboxing getting it to the data type you want it to be.

    • Proposed as answer by IdahoSixString Thursday, November 21, 2013 7:16 PM
    • Edited by IdahoSixString Thursday, November 21, 2013 7:24 PM
    • Marked as answer by Amersh Singh Thursday, November 21, 2013 8:12 PM
    Thursday, November 21, 2013 7:14 PM

All replies

  • ok so first thing is first please next time use code blocks.

    Anyhow your problem is not that you are not casting to string its when you use

    x.Field<string>

    This is a contractual obligation to be this data type so go back and make sure your table or database is the same datatype in which you are saying it is.

    So without knowing exactly which contract is failing I would make sure that you have the correct data types for each of these locations.

    NOTE Areas to check data contract are surrounded with ##.

     var newData = from c in dtnew.AsEnumerable().Where(x => #x.Field<string>#(cbColumnData.SelectedItem.ToString()) != "" && #x.Field<string>#(cbColumnData.SelectedItem.ToString()) != "-")
                                       group c by #c.Field<string>#(cbColumnData.SelectedItem.ToString()) into g
                                       orderby g.ToString()
                                       select new { SelectedColumnName = Convert.ToString(g.Key), DistinctCount =Convert.ToString( g.Count()) };

    So you will want to change those sections to something similar to this without knowing your data types.

    x.Field<double>

    c.Field<double>

    If you are unsure of the datatype you are pulling you can always just say it is object.

    x.Field<object>

    c.Field<object>

    Doing this will add a lot of overhead of boxing and unboxing getting it to the data type you want it to be.

    • Proposed as answer by IdahoSixString Thursday, November 21, 2013 7:16 PM
    • Edited by IdahoSixString Thursday, November 21, 2013 7:24 PM
    • Marked as answer by Amersh Singh Thursday, November 21, 2013 8:12 PM
    Thursday, November 21, 2013 7:14 PM
  • Try code below

            private void button1_Click(object sender, EventArgs e)
            {
                cbColumnData.Items.Add("abc");
                cbColumnData.SelectedIndex = 0;
                DataTable dtnew = new DataTable();
                dtnew.Columns.Add("abc");
                DataRow newRow;
                newRow = dtnew.Rows.Add();
                newRow["abc"] = 123;
                newRow = dtnew.Rows.Add();
                newRow["abc"] = 456;
                newRow = dtnew.Rows.Add();
                newRow["abc"] = 789;
                var newData = dtnew.AsEnumerable()
                        .OrderBy(c => c.Field<object>(cbColumnData.SelectedItem.ToString()))
                        .Where(x => (x.Field<string>(cbColumnData.SelectedItem.ToString()).ToString() != "") && (x.Field<string>(cbColumnData.SelectedItem.ToString()).ToString() != "-"))
                        .GroupBy(c => c.Field<object>(cbColumnData.SelectedItem.ToString()))
                        .ToList()
                        .Select(g => new {SelectedColumnName = g.Key.ToString(), DistinctCount = g.Count()});
            }

    Thursday, November 21, 2013 7:17 PM