8

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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Poku
  • 3,138
  • 10
  • 46
  • 64
  • 3
    Try to use `SqlFunctions.StringConvert((double)afh.addressid)` instead `ToString();` – Renatas M. Nov 30 '11 at 12:23
  • @Poku linq-to-entiites does not support any conversions like conert.Tostring() and convert.ToInt32 , convert.ToDatetime.. – Glory Raj Nov 30 '11 at 12:33
  • @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... – Glory Raj Nov 30 '11 at 12:36
  • but how would i join the data when i can't do that compare? – Poku Nov 30 '11 at 12:39

3 Answers3

3

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.

Community
  • 1
  • 1
Piotr Zierhoffer
  • 5,005
  • 1
  • 38
  • 59
2

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() });
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
1

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");
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
Azhar Khorasany
  • 2,712
  • 16
  • 20