LINQ to DataSet typecast from int to string on joining datatables. RRS feed

  • Question

  • Hi
     I am trying to type cast from integer to string.

    I am using two data tables in a LINQ query, noticed that the columns I am joing on, one is Numeric (getting the data from Oracle DB) other is Varchar (getting data from sql server). what is best bet to write a join on these two columns.

    here is the LINQ query I wrote:

    var linqQuery = from sql1 in sqlDS.Tables["test1"].AsEnumerable()
                            join sql2 in sqlCategoryDS.Tables["test2"].AsEnumerable() on
                            sql1.Field<int>("col1") equals sql2.Field<string>("col1")
                            select new
                                abc = sql1.Field<int>("col1"),
                                xyz = sql2.Field<string>("col2")

    appreciate any help.



    Friday, September 26, 2008 1:50 AM

All replies

  • In this case, this should work


    sql1.Field<int>("col1").ToString() equals sql2.Field<string>("col1")



    In other cases, you could do any method call you want, since this is Linq against objects.


    ConvertToSpaceShip(sql1.Field<int>("col1")) equals ConvertToSpaceShip(sql2.Field<string>("col1"))

    Friday, September 26, 2008 3:13 PM
  • Hi David, thanks for you reply.

    I tried using your first suggestion, but it was giving the error, invalid type cast.


    In the second one, i didn't find any thing like that.

    Friday, September 26, 2008 4:47 PM
  • You must not really have ints or strings in your fields then.  ToString certainly isn't casting.

    Friday, September 26, 2008 5:45 PM

    Specified cast is not valid.  ------- this is the actual error. don't know how debug also.


    when I checked the column contents everything looks okay, all are numeric.



    Friday, September 26, 2008 10:05 PM
  • David,


    [System.InvalidCastException] = {"Specified cast is not valid."}  ------ this is the original error message i am getting.


    don't know how to debug also.


    when I ckecked the column values everything looks okay.


    Thanks for your reply.

    Friday, September 26, 2008 10:07 PM
  • the fields you are joining on must be the same type or have an implicit conversion.



    Tuesday, September 30, 2008 3:59 PM