C# Linq到实体-重用具有导航属性的谓词?

C# Linq到实体-重用具有导航属性的谓词?,c#,linq,entity-framework,navigation-properties,C#,Linq,Entity Framework,Navigation Properties,我试图找到一种在EF 6.1.3中重用谓词来过滤实体的方法。我在使用“Where”筛选相关属性时遇到问题 例如,如果我有此界面IValidFromTo public interface IValidFromTo { DateTime StartDate { get; set;} DateTime EndDate { get; set; } } 以及一个返回谓词的函数,其中: public class Extensions { public static Expression&

我试图找到一种在EF 6.1.3中重用谓词来过滤实体的方法。我在使用“Where”筛选相关属性时遇到问题

例如,如果我有此界面
IValidFromTo

public interface IValidFromTo
{
   DateTime StartDate { get; set;}
   DateTime EndDate { get; set; }
}
以及一个返回
谓词的函数,其中

public class Extensions 
{
  public static Expression<Func<T, bool>> Current<T>() 
  where T : IValidFromTo
  {
    var currentDate = DateTime.Now; 
    return x => x.StartDate <= currentDate && x.EndDate >= currentDate;
  } 
}
我想把它投射到一个包含人名和当前
项的对象中,最后得到一些相当混乱的代码:

var relationQuery = ctx.People.Select(x => new 
    { Name = x.Name,
      CurrentItems = x.Items.AsQueryable().Where(Extensions.Current<Item>())
    });
如果每个
人可能有多个有效
项目
,则

var grouped = peopleWithValid.GroupBy(x => x.Person);
但是,此版本的查询将排除没有匹配
项的人员

var relationQuery = ctx.People.Select(x => new 
    { Name = x.Name,
      CurrentItems = x.Items.AsQueryable().Where(Extensions.Current<Item>())
    });
CurrentItems = x.Items.Current() // quasi an extension method on `ICollection<Item>`?
var isCurrent= x => <<some condition on x>>;
...  
var validItems = ctx.Items.Where(isCurrent);
var peopleWithCurrentItems = from person in ctx.Persons
                                join item in validItems on person.Id equals item.Owner.Id
                                select new { Person = person, Item = item };
var grouped = peopleWithValid.GroupBy(x => x.Person);