Entity Framework Code First 4.3 / LINQKit Predicate for Linked Table

I am using Entity Framework 4.3.1 with the Code First approach. In addition, I use LinqKit to use PredicateBuilder.

If I have tables like:

Location, TimeZone (many: 1)

.. and I want to have something like this:

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 TimeZone, but the Where () method gets the location.

I can rewrite the predicate in the same way, but I donโ€™t want to, because I want to have a factory predicate that creates predicates for certain types (I donโ€™t want to use the Navigator properties this way):

Expression<Func<Location, bool>> pred = PredicateBuilder.True<Location>();
pred = pred.And(loc => loc.TimeZone.TimeZoneCode == "EST");

( ) , , TimeZone, , ( ). , , EF , , .

+5
2

, , . :

  • , (subPredicate)
  • Invoke, subPredicate Predicate (predicate), .
  • Expand 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));

List<Location> locations = context.Locations
    .AsExpandable().Where(pred.Expand())
    .Select(loc => loc).ToList();
+4

: , . : , , . EF, -, .

( , ) : , . , , , -.

, , EXISTS() IN(), " -, - " "" ". , LINQ . EF - -, -, .

, , - , .

.

+1

All Articles