LINQ 到实体 StringConvert(double)'无法翻译为将 int 转换为 string
问题
需要使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
EF 在上层与数据库无关,但处理 linq 查询到 SQL 的转换的部分始终依赖于数据库,并且 SqlFunctions 依赖于 SQL Server Provider,但您使用的是 SQL Server CE 提供程序,该提供程序无法翻译
SqlFunctions
类中的函数。顺便提一句。第三个选项也不是解决方案,因为它会将整个客户表选择到内存中,然后使用 linq-to-objects。你应该使用这个:
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 fromSqlFunctions
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:
这是我现在使用的简化版本(特定于 SQL CE):
注意 db.BlogCategories.AsEnumerable() 部分
Here is a simplified version I'm using now (specific for SQL CE):
Note the db.BlogCategories.AsEnumerable() part