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