本文介绍了EntityFramework投射问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用LinqKit中的PredicateBuilder构建我的查询。
这是伟大的,做我正在寻找的是什么。



为了使我的代码更可重用(表和视图),我创建了一个通用谓词生成器类:

  public class LocalPredicateBuilder< T>其中T:IResort 
...
var predicate = PredicateBuilder.True< T>(


$ b b

这暴露了BuildPredicate方法,我可以这样使用:

  var predicate = new LocalPredicateBuilder& .BuildPredicate(); 
var resorts = _entities.Resorts.Where(predicate).ToList();


$ b b

然而,当我尝试这样做,我得到这个运行时错误(btw实体对象实现IResort):
无法转换类型ConsoleApplication1.Entities.Resort键入'ConsoleApplication1.Entities .IResort'。LINQ to Entities只支持转换实体数据模型原始类型



我试过转换(无效):

  var rlist = eq.Cast< Resort>()。ToList(); 

任何其他方式我可以绕过这个铸造问题吗?



UPDATE >

没有太多运气得到使用接口的谓词工作..所以我

解决方案

误差准确。你不能在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.

这篇关于EntityFramework投射问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:42