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

后端 未结 2 1072
小鲜肉
小鲜肉 2020-12-21 06:06

Problem

Need to convert int to string using EF4 + SQL CE4. The recommended option of using SqlFunctions.StringConvert(double) still gives me errors.

相关标签:
2条回答
  • 2020-12-21 06:26

    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

    0 讨论(0)
  • 2020-12-21 06:30

    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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题