2017-02-02 209 views
0

你好我在这里的对象定义的条款.NET核心实体框架,子对象

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个查询实现这一

+0

题外话:为自己做个忙,为集合使用复数名称。话题上:首先尝试没有包含这些内容的查询。它仍然没有返回正确的结果吗?另外,使用'=='而不是'Equals','Any'而不是'Count()> 0'(表现更好)。 –

+0

我认为,因为您正在使用equals函数,您可能正在评估内存中的where子句,而不是SQL中导致加载的所有内容 – Hamburglar

+0

我知道您正在尝试执行一个查询,但是它可能会更简单地中断查询和EF来填充导航属性?看看这里的例子(https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/read-related-data)。第二个在急切的装载下。 –

回答

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();