Why would Entity Framework not be able to use ToString() in a LINQ statement?

后端 未结 4 1660
执笔经年
执笔经年 2020-12-02 00:05

This works in LINQ-to-SQL:

var customersTest = from c in db.Customers
                select new
                {
                    Id =          


        
4条回答
  •  一向
    一向 (楼主)
    2020-12-02 00:24

    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 :-)

提交回复
热议问题