neerajMAX
neerajMAX

Reputation: 269

LINQ (Unable to cast object of type 'System.Double' to type 'System.String'.)

I am facing a problem while using LINQ concept. below i am comparing two datatables one is filled by database another is by excel file.

Problem is when I am apply join between two "tempdt" contains double value which promoting an error of casting object. I can't change return type from string to double cause it could be any data type in future as currently it is double it might be alpha numeric in future.

    var commonRows = from r1 in dt.AsEnumerable()
                             join r2 in tempdt.AsEnumerable()
                             on r1.Field<string>(0) equals r2.Field<string>(4)
                             select r2;
            if (commonRows.Any())
            {
                abcdefgh = commonRows.Count();
                dt123 = commonRows.CopyToDataTable();
                // ring the ghanta of gutlu
            }

Exception: Unable to cast object of type 'System.Double' to type 'System.String'.

Upvotes: 1

Views: 12194

Answers (5)

Dar&#237;o Le&#243;n
Dar&#237;o Le&#243;n

Reputation: 700

This error is caused by a mismatch datatypes between properties' entity and underlying table columns.

Upvotes: 0

Jens Kloster
Jens Kloster

Reputation: 11277

Here is where I think your problem is:

from r1 in dt.AsEnumerable()
join r2 in tempdt.AsEnumerable() on
r1.Field<string>(0)  //<-- this may not be string
equals 
r2.Field<string>(4) //<-- this may not be string
select r2;

What you could do is to treat it as object:

from r1 in dt.AsEnumerable()
join r2 in tempdt.AsEnumerable() on
(string.Empty + r1.Field<object>(0)) <-- Edited by Andreas X
equals 
(string.Empty + r2.Field<object>(4)) <-- Edited by Andreas X
select r2;

What you should do, is to make sure your index numbers (0 and 4) points to the same type.

Edit: I usually use the old ASP-trick to aboid null pointers when asking to the tostring value.

Upvotes: 6

Andreas Johansson
Andreas Johansson

Reputation: 172

Field expects the underlying datatype to be a string.

see: http://msdn.microsoft.com/en-us/library/bb301394.aspx

InvalidCastException
The value type of the underlying column could not be cast to the type specified by the generic parameter, T.

You could use object to ignore the type as Jens Kloster purposed. However, there is something wrong with your design if you don't know the datagtype of your columns.

Upvotes: 0

neerajMAX
neerajMAX

Reputation: 269

below code is working for me.

thanks every one

    var commonRows = from r1 in dt.AsEnumerable()
                             join r2 in tempdt.AsEnumerable()
                             //on r1.Field<object>(0).ToString() equals r2.Field<object>(4).ToString()
                             on r1[0].ToString() equals r2[4].ToString()
                             select r2;
            if (commonRows.Any())
            {
                abcdefgh = commonRows.Count();
                dt123 = commonRows.CopyToDataTable();
                // ring the ghanta of gutlu
            }

Upvotes: 1

Joachim VR
Joachim VR

Reputation: 2340

Casting to string isn't really possible, since double and string are 2 completely unrelated classes. perhaps try comparing their .ToString() values. Or if you want to be completely safe, compare their string formats, so you don't get any NullReferenceExceptions: string.Format("{0}", fieldvalue)

Upvotes: 2

Related Questions