实体框架不支持 x.ToString()!
实体框架无法识别我的将路由对象转换为指定字符串的 ToString 方法:
public IEnumerable<Route> GetRoutes(string prefix, string searchPattern, string code)
{
return Routes.
Where(x => code == null || x.Code == code).
Where(x => searchPattern == null || x.ToString().Contains(searchPattern)).
Where(x => prefix == null || x.ToString().StartsWith(prefix));
}
这是我的路由实体:
public class Route : IDataEntity, ISoftDeletable
{
public virtual long Id { get; set; }
public virtual string Code { get; set; }
public virtual bool IsDeleted { get; set; }
public virtual Guid CompanyId { get; set; }
public virtual IList<LocationInRoute> Locations { get; set; }
public override string ToString()
{
StringBuilder str = new StringBuilder();
foreach (LocationInRoute loc in Locations)
{
if (str.Length > 0)
{
str.Append(" > ");
}
str.Append(loc.ToString());
}
return str.ToString();
}
}
所有 x.ToString()
都会抛出 linq toEntity 中不支持的异常。 有什么解决办法吗?
Entity framework doesn't recognize my ToString method that converts the route object to specifix string:
public IEnumerable<Route> GetRoutes(string prefix, string searchPattern, string code)
{
return Routes.
Where(x => code == null || x.Code == code).
Where(x => searchPattern == null || x.ToString().Contains(searchPattern)).
Where(x => prefix == null || x.ToString().StartsWith(prefix));
}
Here is my Route entity:
public class Route : IDataEntity, ISoftDeletable
{
public virtual long Id { get; set; }
public virtual string Code { get; set; }
public virtual bool IsDeleted { get; set; }
public virtual Guid CompanyId { get; set; }
public virtual IList<LocationInRoute> Locations { get; set; }
public override string ToString()
{
StringBuilder str = new StringBuilder();
foreach (LocationInRoute loc in Locations)
{
if (str.Length > 0)
{
str.Append(" > ");
}
str.Append(loc.ToString());
}
return str.ToString();
}
}
All the x.ToString()
throws an exception of not supported in linq to entities..
Any workaround?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是一个棘手的解决方案,您可以像这样连接:key is an string property and key it an int?,它不是最佳性能解决方案,但它有效
This is tricky solution you can concat like this: key its an string property and key its an int?, its not the best performance solution but it works
您不能在客户端执行此操作 (
ToString
)。您必须创建将进行评估的 SQL 函数 - 它可以仅对您的位置进行字符串连接(我相信它是相关实体),或者您可以在自定义函数中进行整个测试。然后,您可以将该函数导入实体模型 (EDMX) 并创建
EdmFunctionAttribute
来调用它 - MSDN.You cannot do this on client side (
ToString
).You must create SQL function which will do your evaluation - it can either do only string concatenation of you locations (I believe it is related entity) or you can do the whole test in custom function. Then you can import that function into your entity model (EDMX) and create
EdmFunctionAttribute
to call it - MSDN.这是正确的。实体框架无法将您在 C# 中编写的方法转换为 sql。
实体框架将表达式转换为 sql。有时,这些表达式表示对方法的调用(例如 Queryable.Where ),实体框架知道这些特定的 .net 框架方法以及如何将它们转换为 sql。
您期望数据库如何新建 StringBuilder?
PS:查询中的条件或运算是一种糟糕的方法。您不应该构建一个查询来统治所有这些查询。相反,有条件地构造查询:
That's right. Entity Framework can't convert the methods you write in c# into sql.
Entity Framework converts expressions to sql. Sometimes those expressions represent calls to methods (such as
Queryable.Where
) and Entity Framework knows about those specific .net framework methods and how to translate them into sql.How do you expect the database to new up a StringBuilder?
PS: this or'ing of criteria in the query is a terrible way to go. You shouldn't construct one query to rule them all. Instead, conditionally construct the query:
较新版本的实体框架(从版本 6.1 开始)支持
ToString()
请参阅 EF 6.1 发行说明:
(嗯,这个问题已经有几年了,但也许这些信息可能会帮助其他人......
另请参阅相关问题How to use ToString SelectListItem with Entity Framework?)
ToString()
is supported by newer versions of Entity Framework (from version 6.1 onwards)See Release Notes of EF 6.1:
(Well, the question is already a few years old, but maybe this information might help others...
See also related question How to use ToString SelectListItem with Entity Framework?)
您需要指定
Route
类的哪个属性需要与searchPattern
或prefix
进行比较。您不能在您的场景中隐式执行.ToString()
。You need to specify which property of
Route
class you need to compare withsearchPattern
orprefix
. You cannot implicitly do a.ToString()
in your scenario.我认为你必须有一个名为
ID
或Name
的属性,你想将其与searchPattern
进行比较,然后使用:因为我假设x 展示了一个实体,那么,您希望如何将实体的名称本身与搜索模式进行比较?
编辑:
看到问题的更改后,仍然无法在此查询中使用
ToString()
方法,因为它无法转换为SQL语句。现在,您有两个选择:
第一个:(我不确定是否适用取决于可疑的数据大小),在调用之前尝试使用
ToList()
扩展从数据库加载所有记录Where
扩展名。这应该可以很好地工作,但是对于巨大的表可能会导致问题。
第二个:您必须创建一个存储过程并将逻辑移至数据库。
I think you have to have a preperty called
ID
orName
which you want to compare with thesearchPattern
and then use:Because I'm asuuming that x demonstrate an entity, so, how do you want to compare the entiy's name itself with a search patternn ?
Edit:
After seeing the changes in the question, it's still unable to use
ToString()
method in this query because it's not able to convert to a SQL Statement.Now, you have two options:
The first one: (I'm not sure if applicable depend on the suspected data size), try to load all the records from the database using
ToList()
extension before callingWhere
extension.This should work well but it might cause a problem with huge tables.
The second one: You have to create a stored procedure and move the logic to the database.
这篇文章和这篇文章< /a> 可能会提供一些帮助。请注意,第二个链接中建议的方法不适用于 LINQ to Entities,因为它使用 ToString。为了使其工作,请将 CreateLike 方法替换为:
This post and this post might provide some help. Note that the suggested approach in the second link will not work for LINQ to Entities because it uses ToString. To make it work replace the CreateLike method with: