实体框架代码首先4.3 / LINQKit相关表的谓词 [英] Entity Framework Code First 4.3 / LINQKit predicate for related table
问题描述
如果我有这样的表:
位置,TimeZone(许多:1)
..我希望有这样的东西:
表达式< Func< TimeZone,bool>> pred = PredicateBuilder.True< TimeZone>();
pred = pred.And(tz => tz.TimeZoneCode ==EST);
列表<位置> location = context.Locations
.AsExpandable()。其中(pred)
.Select(loc => loc).ToList();
这不起作用,因为谓词是构建为接受TimeZone,而是Where()方法收到一个位置。
我可以这样重写谓词,但我不想,因为我想要一个谓词工厂,为特定类型创建谓词不想以这种方式使用导航器属性):
表达式< Func< Location,bool>> pred = PredicateBuilder.True< Location>();
pred = pred.And(loc => loc.TimeZone.TimeZoneCode ==EST);
我可以使用什么语法(如果有的话)使用第一个例子中构造的谓词,它需要一个TimeZone,而不是需要一个位置,并通过导航属性走树(因为这是不太可重用)。如果有一种方法可以首先利用EF对导航属性的了解,并且能够使用范围为导航属性类型的谓词。
经过大约一个星期的努力,我发现你其实可以这样做。步骤是:
- 定义一个谓词,它是内部属性的查询(
subPredicate
) -
从另一个谓词(<$)中调用
subPredicate
c $ c> predicate ),反对父对象的属性。 -
展开
您的predicate
当您在Where
子句中使用它时。
这是您的示例的修改代码:
var subPredicate = PredicateBuilder.True< TimeZone>();
subPredicate = subPredicate.And(tz => tz.TimeZoneCode ==EST);
var predicate = PredicateBuilder.True< Location>();
predicate = predicate.And(l => subPredicate.Invoke(l.TimeZone));
列表<位置> locations = context.Locations
.AsExpandable()。其中(pred.Expand())
.Select(loc => loc).ToList();
I am using Entity Framework 4.3.1 with a Code First approach. Also, I am using LinqKit so as to use the PredicateBuilder.
If I have tables like so:
Location, TimeZone (Many:1)
..and I wish to have something like so:
Expression<Func<TimeZone, bool>> pred = PredicateBuilder.True<TimeZone>();
pred = pred.And(tz => tz.TimeZoneCode == "EST");
List<Location> locations = context.Locations
.AsExpandable().Where(pred)
.Select(loc => loc).ToList();
This does not work, because the predicate is built to accept a TimeZone, but the Where() method receives a Location.
I can rewrite the predicate like so, but I do not want to, because I want to have a predicate factory that creates predicates for specific types (I do not want to use the Navigator properties in this manner):
Expression<Func<Location, bool>> pred = PredicateBuilder.True<Location>();
pred = pred.And(loc => loc.TimeZone.TimeZoneCode == "EST");
What syntax could I use (if any) to use the predicate as constructed in the first example, where it takes a TimeZone, rather than have it take a Location and walk the tree via the navigation properties (as this is less reusable). It would be nice if there was a way to leverage the knowledge that EF has about the navigation properties in the first place, and be able to use a predicate scoped to the type of the navigation property.
After about a week's worth of struggling, I found out that you can in fact do this. The steps are:
- Define a Predicate that is the query for your inner property (
subPredicate
) Invoke
thatsubPredicate
from within another Predicate (predicate
), against the property of the parent object.Expand
yourpredicate
when using it in yourWhere
clause.
Here's the revised code for your example:
var subPredicate = PredicateBuilder.True<TimeZone>();
subPredicate = subPredicate.And(tz => tz.TimeZoneCode == "EST");
var predicate = PredicateBuilder.True<Location>();
predicate = predicate.And(l => subPredicate.Invoke(l.TimeZone));
List<Location> locations = context.Locations
.AsExpandable().Where(pred.Expand())
.Select(loc => loc).ToList();
这篇关于实体框架代码首先4.3 / LINQKit相关表的谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!