LINQ to Entities StringConvert(double)'不能转换为将int转换为字符串 [英] LINQ to Entities StringConvert(double)' cannot be translated to convert int to string

查看:264
本文介绍了LINQ to Entities StringConvert(double)'不能转换为将int转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

需要使用EF4 + SQL CE4将int转换为字符串。使用SqlFunctions.StringConvert(double)的建议选项仍然给我错误。

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

选项1 (原始)。这给我错误:

Option 1 (original). This gives me error:

public static IEnumerable<SelectListItem> xxGetCustomerList()
    {
        using (DatabaseEntities db = new DatabaseEntities())
        {
            var list = from l in db.Customers
                       orderby l.CompanyName
                       select new SelectListItem { Value = l.CustomerID.ToString(), Text = l.CompanyName };
            return list.ToList();
        }
    }

选项2 建议)。然后尽可能多的帖子建议,我使用库System.Data.Objects.SqlClient中的SqlFunctions.StringConvert()函数:

Option 2 (most suggested). Then as many posts suggests, I used the SqlFunctions.StringConvert() function from the library System.Data.Objects.SqlClient:

public static IEnumerable<SelectListItem> GetCustomerList()
    {
        using (DatabaseEntities db = new DatabaseEntities())
        {
            var list = from l in db.Customers
                       orderby l.CompanyName
                       select new SelectListItem { Value = SqlFunctions.StringConvert((double)l.CustomerID), Text = l.CompanyName };
            return list.ToList();
        }
    }

现在显示如下错误:

The specified method 'System.String StringConvert(System.Nullable`1[System.Double])' on the type 'System.Data.Objects.SqlClient.SqlFunctions' cannot be translated into a LINQ to Entities store expression.

选项3 (对于非常具体的情况)。然后anoter post显示一个使用Dictionary的智能解决方案,最终可以使用:

Option 3 (for very specific case). Then anoter post shows a smart solution using Dictionary, which finally works:

public static IEnumerable<SelectListItem> xGetCustomerList()
    {
        using (DatabaseEntities db = new DatabaseEntities())
        {
            var customers = db.Customers.ToDictionary(k => k.CustomerID, k => k.CompanyName);
            var list = from l in customers
                       orderby l.Value
                       select new SelectListItem { Value = l.Key.ToString(), Text = l.Value };
            return list.ToList();
        }
    }

但只适用于简单的对值(键值, )。有人可以通过另一个解决方案来帮助我吗?或者我在选项2中做错了什么?

But only work for simple pair values (key, value). Can someone help me with another solution or what I'm doing wrong with option 2?

我希望微软很快就会使EF正确,然后再推动我们从L2S已经稳定,更成熟。我实际上使用EF4只是因为想要使用SQL CE,否则我会留在L2S。

And I hope Microsoft will soon make EF right before pushing us to move from L2S which is already stable and much more mature. I actually using EF4 just because want to use SQL CE, otherwise I stay with L2S.

推荐答案

EF在上层是独立于数据库但是处理LINQ查询转换为SQL的部分总是依赖于数据库,而 SqlFunctions 依赖于SQL Server提供程序,但您使用的SQL Server CE提供程序无法翻译函数从 SqlFunctions 类。

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。第三个选项也不是一个解决方案,因为它会选择整个客户表到内存,然后使用linq对对象。你应该使用这个:

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();
    }
}

这篇关于LINQ to Entities StringConvert(double)'不能转换为将int转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆