0

I have this query expression, to concatenate some values for data validation

result.AddRange(from string dr in ercDt
   select dc.Ordinal.ToString() + "|" + dr);

Having that ercDt[0] is DBNull, and has no more rows, i'm getting this error

Unable to cast object of type 'System.DBNull' to type 'System.String'.

I've tried this

result.AddRange(from string dr in ercDt
where dr.Any(x => x != null)
select dc.Ordinal + "|" + dr);

and this

result.AddRange(from string dr in ercDt
where ercDt.Any(x => x != null)
select dc.Ordinal + "|" + dr);

and this

result.AddRange(from string dr in ercDt
where !(dr is DBNull)
select dc.Ordinal + "|" + dr);

And no luck,

How can I prevent this error from happening? I need to query no nulls at all, so my desired output in this case would be no records.

Thanks

EDIT:

Based on the confusion I'll describe better my case:

'ercDt' is a List coming from a linq query (the result of that linq query is strings and very rare cases of DBNull) converted to List (with ToList() method), so I can cast 'dr' to string, but my problems comes from the DBNull values

culebrin
  • 125
  • 1
  • 15
  • If `dr` is a `DataRow` that doesnt work anyway. You are casting the `DataRow` to `String` which should give you a runtime error. What is `ercDt`(it it was a `DataTable` you'd get a compiler error)? What are you actually trying to do? – Tim Schmelter Feb 18 '15 at 14:56
  • Thanks Tim, ercDt is a List but those objects I assumed as strings (result of a linq query from a datatable and converted with .ToList() method) that's why I cast as string straight from ercDt. What I'm trying to do is concatenate the contents from ercDt to the ordinal of a higher level datatable (dc is from another Datatable) – culebrin Feb 18 '15 at 15:06

2 Answers2

0

If dr is a DataRow that doesnt work anyway. You are casting the DataRow to String which should give you a runtime error. What is ercDt(it it was a DataTable you'd even get a compiler error)? I'll give it a try anyway.

So presuming that ercDt is a DataTable, i guess you want all fields with a value, so those which are not DBNull.Value. Use DataRow.IsNull:

DataColumn dc = ercDt.Columns[0]; // i don't know which column you use
var fields = from dr in ercDt.AsEnumerable()
             where !dr.IsNull(dc.Ordinal)
             select dc.Ordinal + "|" + dr[dc].ToString();
result.AddRange(fields);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Thanks Tim, ercDt is a List but those objects I assumed as strings (result of a linq query from a datatable and converted with .ToList() method) that's why I cast as string straight from ercDt. What I'm trying to do is concatenate the contents from ercDt to the ordinal of a higher level datatable (dc is from another Datatable) – culebrin Feb 18 '15 at 15:08
  • @culebrin: maybe my answer is helpful anyway. – Tim Schmelter Feb 18 '15 at 15:10
0

I would do this:

result.AddRange(from string dr in ercDt
select ((dc.Ordinal == null) ? string.Empty() : dc.Ordinal.ToString()) + 
    "|" + ((dr == null) ? string.Empty() : dr));
Scottie
  • 11,050
  • 19
  • 68
  • 109
  • Thanks Scottie, your answer gives me an idea of restrict the list before querying it. I'll let you know... – culebrin Feb 18 '15 at 15:32