8
votes

i'm trying to compare a int with a string in the join method of linq lambda, like this:

database.booking.Join(database.address,
                      book => book.bookno,
                      afh => afh.addressid.ToString(),
                       (book, afh) => new { booking = book, add = afh })
                .Where(book => book.address.name == "test");

but i'm getting an error on the ToString():

System.NotSupportedException: LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression.

How do i solve this?

3
Try to use SqlFunctions.StringConvert((double)afh.addressid) instead ToString();Renatas M.
@Poku linq-to-entiites does not support any conversions like conert.Tostring() and convert.ToInt32 , convert.ToDatetime..Enigma State
@Poku My best suggestion is first retrive the records from database into list and then do conversion and comparison on that list ..then it works for you...Enigma State
but how would i join the data when i can't do that compare?Poku

3 Answers

3
votes

Are you working with Linq to SQL? Linq is trying to convert your lambda to sql query. Unfortunately, ToString is not so easily supported.

You can materialize your tables with ToArray() before join, but it can be expensive.

Look at this article and this question.

2
votes

Try this:

var bookinger = database.booking.Join(database.address,
                         book => book.bookno,
                         afh => afh.addressid,
                         (book, afh) =>
                         new { booking = book, add = afh })
                     .Where(book => book.address.name == "test")
                     .Select(new { booking, add = add.ToString() });
1
votes

Have you tried this??

var bookinger = 
    database.booking.Join(database.address,
        book => book.bookno,
        afh => Convert.ToString(afh.addressid),
        (book, afh) =>
        new { booking = book, add = afh })
    .Where(book => book.address.name == "test");