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

后端 未结 2 1074
小鲜肉
小鲜肉 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: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 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();
        }
    }
    

提交回复
热议问题