简体   繁体   English

LINQ to Entities无法识别表达式中的方法

[英]LINQ to Entities does not recognize method inside an Expression

I ran into a conceptional problem using EntityFramework/"Linq to Entities". 我遇到了一个使用EntityFramework /“ Linq to Entities”的概念性问题。

Say, we have a table and we're doing some filtering-operations based on a column called "ValidTo" (Type: DateTime?) we will probably write a query that looks like this: 说,我们有一个表,并且正在基于名为“ ValidTo”(类型:DateTime?)的列进行一些过滤操作,我们可能会编写一个如下查询:

  EntityCollection/*IQueryable<Assignment>*/.SingleOrDefault(o => o.ValidTo == null);

This simplified example works just fine. 这个简化的示例很好用。 But as the application grows we probably need the same logic for different tables and we want to refactor and separate the business-logic from the context of a specific entity to ensure we dont violate the golden DRY principle. 但是随着应用程序的增长,我们可能需要为不同的表使用相同的逻辑,并且我们希望将业务逻辑与特定实体的上下文进行重构和分离,以确保我们不违反黄金DRY原则。

This time we will have to get the name of the column at runtime because the type is generic and therefore the column is obviously unknown at compile time. 这次,我们将必须在运行时获取列的名称,因为类型是通用的,因此在编译时该列显然是未知的。

I use reflection to get the column name at runtime. 我使用反射在运行时获取列名。 The code ended up looking like this: 代码最终看起来像这样:

  EntityCollection/*IQueryable<TEntity>*/.SingleOrDefault(o => typeof(TEntity).GetProperty(_efRangeEndPropName /* ValidTo or whatever... */).GetValue(o) == null);

Note: The line above will throw an exception! 注意:以上行将引发异常!

An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

Additional information: LINQ to Entities does not recognize the method 'System.Object GetValue(System.Object)' method, and this method cannot be translated into a store expression.

From what I understand about this exception Entity Framework can't parse this expression into a SQL-query. 据我对这种异常的了解,实体框架无法将此表达式解析为SQL查询。 My current workaround is to load the objects in memory before filtering the data: 我当前的解决方法是在过滤数据之前将对象加载到内存中:

var entityList = /*IQueryable<TEntity>*/EntityCollection.ToList();
entityList.SingleOrDefault(o => typeof(TEntity).GetProperty(_efRangeEndPropName).GetValue(o) == null);

This code works. 此代码有效。 However - you guessed it - I'm still not happy. 但是-您猜对了-我还是不开心。 While this might be okay when running the query against small datasets, performance issues with larger datasets are unavoidable this way (the entire dataset has to be loaded in memory first!) 虽然对小型数据集运行查询时可以这样做,但通过这种方式不可避免地会遇到大型数据集的性能问题(必须首先将整个数据集加载到内存中!)

How do I need to structure the expression in a way Entity Framework "understands" how to translate it into a SQL query? 我该如何以实体框架“理解”如何将表达式转换为SQL查询的方式来构造表达式? I bet I am missing something very simple here... 我敢打赌我在这里错过了一些非常简单的事情...

Thanks in advance 提前致谢

How do I need to structure the expression in a way Entity Framework "understands" how to translate it into a SQL query? 我该如何以实体框架“理解”如何将表达式转换为SQL查询的方式来构造表达式?

Instead of reflection, you should build it using the Expression class methods: 代替反射,应该使用Expression类方法构建它:

var parameter = Expression.Parameter(typeof(TEntity), "o"); // o =>
var left = Expression.PropertyOrField(parameter, _efRangeEndPropName); // o.Property
var right = Expression.Constant(null, left.Type); // null
var condition = Expression.Equal(left, right); // o.Property == null
var predicate = Expression.Lambda<Func<TEntity, bool>>(condition, parameter);

You can put the above in a separate method if you need it in a more that one place. 如果您需要在一个以上的地方,可以将以上内容放在单独的方法中。 The result expression ( predicate variable) is EF compatible, as you can see: 结果表达式( predicate变量)与EF兼容,如您所见:

var result = EntityCollection.SingleOrDefault(predicate);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LINQ to Entities无法识别该方法,无法将Method转换为存储表达式 - LINQ to Entities does not recognize the method, Method cannot be translated into a store expression LINQ to Entities无法识别该方法,并且该方法无法转换为商店表达式 - LINQ to Entities does not recognize the method ,and this method cannot be translated into a store expression LINQ to Entities无法识别方法…,并且该方法无法转换为商店表达式 - LINQ to Entities does not recognize the method …, and this method cannot be translated into a store expression LINQ to Entities无法识别该方法,并且该方法无法转换为商店表达式 - LINQ to Entities does not recognize the method, and this method cannot be translated into a store expression LINQ to Entities无法识别该方法,并且该方法无法转换为商店表达式 - LINQ to Entities does not recognize the method and this method cannot be translated into a store expression Linq To Entities 异常无法识别方法,无法翻译成store表达式 - Linq To Entities exception does not recognize the method and cannot be translated into store expression linq to实体无法识别该方法 - linq to entities does not recognize the method LINQ to Entities无法识别该方法 - LINQ to Entities does not recognize the method LINQ to Entities无法识别该方法 - LINQ to Entities does not recognize the method LINQ to Entities无法识别该方法 - LINQ to Entities does not recognize the method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM