LINQ 到实体 StringConvert(double)'无法翻译为将 int 转换为 string

发布于 2024-11-03 06:26:32 字数 2123 浏览 0 评论 0原文

问题

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

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

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() 函数:

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(对于非常具体的情况)。然后另一篇文章展示了一个使用字典的智能解决方案,它最终起作用了:

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 中做错了什么?

我希望 Microsoft 在推动我们从已经稳定且更加成熟的 L2S 转向之前尽快推出 EF。我实际上使用 EF4 只是因为想使用 SQL CE,否则我会继续使用 L2S。

Problem

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

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

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

Which now shows below error:

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.

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

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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情独悲 2024-11-10 06:26:32

EF 在上层与数据库无关,但处理 linq 查询到 SQL 的转换的部分始终依赖于数据库,并且 SqlFunctions 依赖于 SQL Server Provider,但您使用的是 SQL Server CE 提供程序,该提供程序无法翻译 SqlFunctions 类中的函数。

顺便提一句。第三个选项也不是解决方案,因为它会将整个客户表选择到内存中,然后使用 linq-to-objects。你应该使用这个:

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

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();
    }
}
自此以后,行同陌路 2024-11-10 06:26:32

这是我现在使用的简化版本(特定于 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();
        }
    }

注意 db.BlogCategories.AsEnumerable() 部分

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文