Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ lambda - convert int to string

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?

like image 227
Poku Avatar asked Nov 30 '11 12:11

Poku


3 Answers

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.

like image 93
Piotr Zierhoffer Avatar answered Nov 16 '22 01:11

Piotr Zierhoffer


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() });
like image 27
Andrew Hare Avatar answered Nov 16 '22 00:11

Andrew Hare


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");
like image 1
Azhar Khorasany Avatar answered Nov 16 '22 00:11

Azhar Khorasany