问题描述
我正在使用LinqKit的PredicateBuilder构建我的查询。它是伟大的,做我正在寻找的。
为了使我的代码更可重用(表和视图),我创建了一个泛型谓词构建器类:
public class LocalPredicateBuilder< T>其中T:IResort
...
var predicate = PredicateBuilder.True< T>(
它暴露了BuildPredicate方法,我可以这样使用:
var predicate = new LocalPredicateBuilder< Resort>() .BuildPredicate();
var resort = _entities.Resorts.Where(predicate).ToList();
但是,当我尝试这样做时,我得到这个运行时错误(btw实体对象实现IResort):
无法将类型ConsoleApplication1.Entities.Resort转换为ConsoleApplication1.Entities .IResort'。LINQ to Entities只支持投射实体数据模型原始类型
我试过投射(没有工作):
var rlist = eq.Cast< Resort>()。ToList();
更新
>
没有很多运气得到谓词使用界面工作,所以我
解决方案嗯,错误是准确的。您不能在L2E查询中执行此操作,因为您的界面(
IReport
)不是您的实体模型的一部分,因此无法转换为SQL。你必须使用一个实体类型,而不是一个接口。I am building my query using PredicateBuilder from LinqKit. it is great and does exactly what i am looking for.
To make my code more reusable (tables and views) i created a generic predicate builder class:
public class LocalPredicateBuilder<T> where T : IResort ... var predicate = PredicateBuilder.True<T>(
which exposes BuildPredicate method. I can use it like this:
var predicate = new LocalPredicateBuilder<Resort>().BuildPredicate(); var resorts = _entities.Resorts.Where(predicate).ToList();
however when i try to do this, i get this runtime error (btw entity objects implement IResort):Unable to cast the type 'ConsoleApplication1.Entities.Resort' to type 'ConsoleApplication1.Entities.IResort'. LINQ to Entities only supports casting Entity Data Model primitive types
i tried casting (didn't work):
var rlist = eq.Cast<Resort>().ToList();
Any other way i can get around this casting issue?
UPDATE
not having much luck getting predicates to work using interfaces.. so i solved my problem by going with POCOs.
解决方案Well, the error is accurate. You can't do that in an L2E query, because your interface (
IReport
) is not part of your entity model and hence can't be converted to SQL. You have to use an entity type, not an interface for that.这篇关于实体框架铸造问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!