LINQ to Entities StringConvert(double)' cannot be translated to convert int to string

若如初见. 提交于 2019-11-29 15:17:33

EF is database independent at upper layers but the part dealing with conversion of linq query to SQL is always database dependent and SqlFunctions are dependent on SQL Server Provider but you are using SQL Server CE provider which is not able to translate functions from SqlFunctions class.

Btw. third option is also not a solution because it will select whole customer table to memory and use linq-to-objects after that. You should use this:

public static IEnumerable<SelectListItem> xxGetCustomerList()
{
    using (DatabaseEntities db = new DatabaseEntities())
    {
        // Linq to entities query
        var query = from l in db.Customers
                    orderby l.CompanyName
                    select new { l.CustomerID, l.CompanyName };

        // Result of linq to entities transformed by linq to objects
        return query.AsEnumerable()
                    .Select(x => new SelectListItem
                        {
                           Value = x.CustomerID.ToString(),
                           Test = x.CompanyName  
                        }).ToList();
    }
}

Here is a simplified version I'm using now (specific for SQL CE):

    public static IEnumerable<SelectListItem> GetBlogCategoryList()
    {
        using (SiteDataContext db = new SiteDataContext())
        {
            var list = from l in db.BlogCategories.AsEnumerable()
                       orderby l.CategoryName
                       select new SelectListItem { Value = l.CategoryID.ToString(), Text = l.CategoryName };

            return list.ToList();
        }
    }

Note the db.BlogCategories.AsEnumerable() part

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!