.net核心实体框架子对象的Where子句

时间:2017-02-02 18:40:00

标签: .net entity-framework asp.net-core

您好我的对象定义

List<SeasonBasket> baskets = base.Factory.Context.SeasonBasket
.Include (s=> s.Season)
    .ThenInclude (dt => dt.DivisionTeam)
.Include(sbl => sbl.SeasonBasketLocalisation)
.Include(sbm => sbm.SeasonBasketMatch)
    .ThenInclude(m => m.Match)
        .ThenInclude(p => p.Period)
.ThenInclude(pt => pt.PeriodType)
    .ThenInclude(ptl => ptl.PeriodTypeLocalisation)
.Include(sbm => sbm.SeasonBasketMatch)
    .ThenInclude(m => m.Match)
        .ThenInclude(p => p.Period)
            .ThenInclude(c => c.Challenge)
                .ThenInclude(cl => cl.ChallengeLocalisation)
.Include(sbm => sbm.SeasonBasketMatch)
    .ThenInclude(sf => sf.SeasonFight)
        .ThenInclude(tf => tf.TeamFight)
            .ThenInclude(t => t.Team)        
                .ThenInclude(dt => dt.DivisionTeam)        
.Include(sbm => sbm.SeasonBasketMatch)
    .ThenInclude(sf => sf.SeasonFight)
        .ThenInclude(sfpr => sfpr.SeasonFightPeriodResult)   
.Where(x =>
       x.Season.DivisionTeam.Where(y=> y.TeamId.Equals(TeamId) && y.SeasonId.Equals(seasonId)).Count() > 0
       &&
       x.SeasonId.Equals(seasonId)
       )

.ToList();

我的问题是如果子对象不包含指定的teamId,它会加载所有父对象事件。

我的问题是如何在1个查询中实现此目的

1 个答案:

答案 0 :(得分:0)

修改

现在这应该会为您提供您想要获得的所有数据,但它不会为您提供SeasonBasket和FYI的列表,这将是一个非常昂贵的查询。

List<DivisionTeam> divisionTeams = base.Factory.Context.DivisionTeam
       .Include(dt => dt.Season)
           .ThenInclude(s => s.SeasonBasket)
           .ThenInclude(sb => sb.SeasonBasketMatch)
           .ThenInclude(sbm => sbm.Match)
           .ThenInclude(m => m.Period)
           .ThenInclude(pt => pt.PeriodType)
           .ThenInclude(ptl => ptl.PeriodTypeLocalisation)
       .Include(dt => dt.Season)
           .ThenInclude(s => s.SeasonBasket)
           .ThenInclude(sb => sb.SeasonBasketMatch)
           .ThenInclude(m => m.Match)
           .ThenInclude(p => p.Period)
           .ThenInclude(c => c.Challenge)
           .ThenInclude(cl => cl.ChallengeLocalisation)
       .Include(dt => dt.Season)
           .ThenInclude(s => s.SeasonBasket)
           .ThenInclude(sb => sb.SeasonBasketMatch)
           .ThenInclude(sf => sf.SeasonFight)
           .ThenInclude(tf => tf.TeamFight)
           .ThenInclude(t => t.Team)        
           .ThenInclude(dt => dt.DivisionTeam) 
       .Include(dt => dt.Season)
           .ThenInclude(s => s.SeasonBasket)
           .ThenInclude(sb => sb.SeasonBasketMatch)
           .ThenInclude(sf => sf.SeasonFight)
           .ThenInclude(sfpr => sfpr.SeasonFightPeriodResult)
       .Include(dt => dt.Season)
           .ThenInclude(s => s.SeasonBasket)
           .ThenInclude(sb => sb.SeasonBasketMatch)
           .ThenInclude(sbl => sbl.SeasonBasketLocalisation)
       .Where(y => y.TeamId == TeamId && y.SeasonId == seasonId)
       .ToList();