Entity Framework Code First 4.3 / LINQKit predicate for related table

前端 未结 2 1993
一生所求
一生所求 2021-01-03 03:25

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,

2条回答
  •  死守一世寂寞
    2021-01-03 03:50

    After about a week's worth of struggling, I found out that you can in fact do this. The steps are:

    1. Define a Predicate that is the query for your inner property (subPredicate)
    2. Invoke that subPredicate from within another Predicate (predicate), against the property of the parent object.
    3. Expand your predicate when using it in your Where clause.

    Here's the revised code for your example:

    var subPredicate = PredicateBuilder.True();
    subPredicate = subPredicate.And(tz => tz.TimeZoneCode == "EST");
    
    var predicate = PredicateBuilder.True();
    predicate = predicate.And(l => subPredicate.Invoke(l.TimeZone));
    
    List locations = context.Locations
        .AsExpandable().Where(pred.Expand())
        .Select(loc => loc).ToList();
    

提交回复
热议问题