0

I'm new to Linq, the below line gives me "Unable to cast object of type 'System.Double' to type 'System.String'". How can I resolve it?

dt.AsEnumerable().Where(dataRow => !string.IsNullOrEmpty(dataRow.Field<string>(dc.ColumnName).First().ToString()) && (dataRow.Field<int>(dc.ColumnName) == 1)).Count() > 3

I'm querying a DataTable column.

Lord-David
  • 535
  • 1
  • 11
  • 34

4 Answers4

2

The row which you are trying to cast as a string is having data type as double. Instead of dataRow.Field<string>(dc.ColumnName), it should be dataRow.Field<double>(dc.ColumnName)

Chris
  • 27,210
  • 6
  • 71
  • 92
  • And what's the difference between `dataRow.Field(dc.ColumnName)` and `dataRow.Field(dc.ColumnName)`? – Razvan Dumitru Mar 20 '17 at 10:00
  • 3
    The "fix" isn't a fix (and doesn't even compile) and the explanation is wrong. It's not the row that has a type, it's the field – Panagiotis Kanavos Mar 20 '17 at 10:01
  • If you want to include code in your answer you should use appropriate code markup. There should be a button at the top of the editor that you can use - just highlight the text and hitht he code button and it should mark it up. If you can't do that for some reason then the two ways to do code are indenting by four spaces if it is on its own lines or if it is inline like this then using backticks (````) around the code sections. I'll edit this post but thought I'd let you know for the future. The reason to do this is because it won't then try to interpret anything you put in as HTML for example. – Chris Mar 20 '17 at 10:11
  • Thanks Chris. I am new to stack overflow & i didn't know that one. Thank you for guiding me. I really appriciate that. –  Mar 20 '17 at 10:30
2

I suspect you encountered a NullReferenceException while trying to filter for rows that are equal to 1. To avoid this, return a nullable type with Field<double?>(), eg:

dt.AsEnumerable()
  .Where(dataRow => dataRow.Field<double?>(dc.ColumnName) == 3m))
  .Count() > 3

The comparison will fail if the field is null.

If you want to retrieve the column's value while converting the NULL to eg 0, you can use the ?? operator:

.Select( dataRow => dataRow.Field<double?>(dc.ColumnName) ?? 0m)
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
0

First you can refer to this Answer, but i will try to simplify this problem.

If the value of the specified DataColumn is null and T is a reference type or nullable type, the return type will be null. The Field method will not return Value.

DataRowExtensions.Field<T> Method (DataRow, String)

it appeared in .NET 3.5 and it provides strongly-Typed access to every column in rows, and also support types.

so you can use Convert.ToDouble(row["dc.ColumnName"]) to avoid the nullable value returns.

Community
  • 1
  • 1
-1

I know this is old but I wanted to drop my answer here for anyone else. I was trying to create a POCO class for a stored procedure and I was using floats instead of doubles for some of the returned values. Just make sure your types are correct.

Darth Scitus
  • 490
  • 1
  • 9
  • 21