Why can't ToString() be used in a LINQ query by Entity Framework?

c# entity-framework linq linq-to-sql


In LINQ-to-SQL, this code reads:

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)

However, a comparable example in Entity Framework receives a message of error error, which essentially states that it cannot "convert it to SQL".

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


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

Anyone with information on how to make this sort of statement operate in Entity Framework or an explanation of why it encounters this issue is welcome to share it.

12/17/2009 10:28:02 AM

Popular Answer

To put it simply, LINQ to Entities is unaware of the transformation of your ID type into a string.

What category ofc.ID ? Is there a reason why ID uses one kind and credit cards use anotherReferenzId ? Make them the same kind if at all feasible, and then you won't have an issue. Although aligning the types might be nicer, I'm not sure if there are any other techniques to convert data in LINQ to Entities.

By the way, this seems to be a join in every sense:

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: In response to what you said,ToString Since the compiler is unsure of what your query will imply or how it will be interpreted, it will display in IntelliSense. Although C# is absolutely legitimate and can produce a valid expression tree, EF is unable to translate that expression tree into SQL.

You might do so by usingConvert.ToString(c.Id) rather of just dialingc.Id.ToString() ...

12/17/2009 10:32:55 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow