Entity Framework - Include Multiple Levels of Properties

前端 未结 8 866
囚心锁ツ
囚心锁ツ 2020-11-22 08:43

The Include() method works quite well for Lists on objects. But what if I need to go two levels deep? For example, the method below will return ApplicationServers with the i

8条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 09:07

    For EF 6

    using System.Data.Entity;
    
    query.Include(x => x.Collection.Select(y => y.Property))
    

    Make sure to add using System.Data.Entity; to get the version of Include that takes in a lambda.


    For EF Core

    Use the new method ThenInclude

    query.Include(x => x.Collection)
         .ThenInclude(x => x.Property);
    

提交回复
热议问题