C# Entity Framework 4.1 Lambda Include - only select specific included values

前端 未结 3 761
北恋
北恋 2021-01-22 00:47

I\'m doing a lambda select on EF4.1, including another related DBSet in my current statement.

 return dbEntity.GameTypes.Include(a => a.Draws)
                        


        
相关标签:
3条回答
  • 2021-01-22 01:00

    From MSDN for DbExtensions.Include().

    The path expression must be composed of simple property access expressions together with calls to Select in order to compose additional includes after including a collection property.

    So I don't think using Where() is allowed. I'm not sure if you can do any filtering when it comes to Include().

    0 讨论(0)
  • 2021-01-22 01:23

    I think....

        from g in dbEntity.GameTypes.Include("Draws")
       where g.IsActive
         let d = g.Draws.Where(o => o.DrawDate > System.DateTime.Now)
                        .OrderBy(o => o.DrawDate)
                        .Take(1)       // Needs to stay a collection
      select new GameType {IsActive = g.IsActive, Draws = d}
    

    untested - but it might get you on the right path...

    0 讨论(0)
  • 2021-01-22 01:25

    I've never been successful at finding a way to filter the children like you want to do. In order to reduce the volume of data being retrieved from the database I will usually retrieve just the parent objects and then loop over them getting only the children I want and "attach" them to the parent. That's not a hard-and-fast rule though, it kind of depends on how many children there are for each parent and what percentage of them I'm likely to want to keep.

    0 讨论(0)
提交回复
热议问题