views:

185

answers:

2

This works in LINQ-to-SQL:

var customersTest = from c in db.Customers
                select new
                {
                    Id = c.Id,
                    Addresses = from a in db.Addresses where c.Id.ToString() == 
                        a.ReferenzId select a
                };

foreach (var item in customersTest)
{
    Console.WriteLine(item.Id);
}

But a similar example in Entity Framework gets an error message that says basically that it can't "translate it to SQL", here is the original error message in German:

"'LINQ to Entities' erkennt die Methode 'System.String ToString()' nicht, und diese Methode kann nicht in einen Speicherausdruck übersetzt werden."

Translation:

"'LINQ to Entities' does not recognize Method 'System.String ToString()', this method can not be translated into a memory expression.

Can anyone shed any light on how we could get this kind of statement to work in Entity Framework or explain why it gets this error?

+2  A: 

Simply put: LINQ to Entities doesn't know about the conversion from your ID type to a string.

What is the type of c.ID? Is there any reason why it's one type for ID, but another for ReferenzId? If at all possible, make them the same type, at which point you won't have a problem any more. I don't know if there are other ways of performing conversions in LINQ to Entities - there may be - but aligning the types would be cleaner.

By the way, this really looks like it's a join:

var query = from c in db.Customers
            join a in db.Addresses on c.Id equals a.ReferenzId into addresses
            select new { Id = c.Id, Addresses = addresses };

EDIT: To respond to your comment - ToString appears in IntelliSense because the compiler has no real idea what your query is going to mean or how it will be translated. It's perfectly valid C#, and can generate a valid expression tree - it's just that EF doesn't know how to convert that expression tree into SQL.

You could try using Convert.ToString(c.Id) instead of just calling c.Id.ToString()...

Jon Skeet
it's a historical database that we are using where a.ReferenzID is indeed a string, the reasons being that it can be joined to an int in one table but a GUID in another.
Edward Tanguay
thanks but Convert.ToString(c.Id) gives us the same error
Edward Tanguay
Try converting the other way round? Call `new Guid(a.ReferenzId)` instead?
Jon Skeet
I doubt the parameterized constructor will work, but I'm thinking that `String.Equals` or `Guid.Equals` might.
Craig Stuntz
A: 

LINQ to Entities as far as I understand it (for v1) is very primative. In otherwords it doesn't know how to take the extension method "ToString()" and generate the SQL for it.

In LINQ to SQL, it executes the extension method "ToString()" before generating the SQL. The difference is that LINQ to Entities uses IQueryable instead of IEnumerable.

BUT, from what I remember casting should work (because casting is a data type and SQL knows about CAST()).

So

c.Id.ToString() should really be (string)c.Id

(also, make sure it is (string) and not (String)).

One of the downfalls I would say about using Lambda (in Entity Framework) to generate the SQL expression instead of pure LINQ.

Keep in mind too, that using CAST on the left side of the equals sign in SQL is a bit ill performing :-)

jwendl