178

好的,我有具有以下层次结构的三级实体:课程->模块->章节

这是原始的 EF LINQ 语句:

Course course = db.Courses
                .Include(i => i.Modules.Select(s => s.Chapters))
                .Single(x => x.Id == id); 

现在,我想包含另一个与课程相关联的名为 Lab 的实体。

如何包含 Lab 实体?

我尝试了以下但没有奏效:

Course course = db.Courses
                .Include(i => i.Modules.Select(s => s.Chapters) && i.Lab)
                .Single(x => x.Id == id); 

关于包括第二个实体的任何想法?

任何建议或信息将不胜感激。谢谢!

4

6 回答 6

258

您是否尝试过添加另一个Include

Course course = db.Courses
                .Include(i => i.Modules.Select(s => s.Chapters))
                .Include(i => i.Lab)
                .Single(x => x.Id == id);

您的解决方案失败,因为Include不采用布尔运算符

Include(i => i.Modules.Select(s => s.Chapters) &&          i.Lab)
                           ^^^                  ^             ^ 
                          list           bool operator    other list

更新 要了解更多信息,请下载LinqPad并查看示例。我认为这是熟悉 Linq 和 Lambda 的最快方法。

Select首先 -和之间的区别在于Include,使用 Select 您可以决定返回的内容(也称为投影)。Include 是一个Eager Loading函数,它告诉 Entity Framework 您希望它包含来自其他表的数据。

Include 语法也可以是字符串。像这样:

           db.Courses
            .Include("Module.Chapter")
            .Include("Lab")
            .Single(x => x.Id == id);

LinqPad中的示例更好地解释了这一点。

于 2013-04-02T12:55:26.453 回答
73

在 Entity Framework Core ( EF.core) 中,您可以使用.ThenInclude包含下一个级别。

var blogs = context.Blogs
    .Include(blog => blog.Posts)
        .ThenInclude(post => post.Author)
    .ToList();

更多信息:https ://docs.microsoft.com/en-us/ef/core/querying/related-data

注意: 假设您需要多个ThenInclude()on blog.Posts,只需重复Include(blog => blog.Posts)并执行另一个ThenInclude(post => post.Other)

var blogs = context.Blogs
    .Include(blog => blog.Posts)
        .ThenInclude(post => post.Author)
    .Include(blog => blog.Posts)
        .ThenInclude(post => post.Other)
 .ToList();
于 2017-11-29T15:04:23.567 回答
24

Include是fluent interface的一部分,所以你可以写多个Include语句,一个接一个

 db.Courses.Include(i => i.Modules.Select(s => s.Chapters))
           .Include(i => i.Lab)
           .Single(x => x.Id == id); 
于 2013-04-02T12:55:59.893 回答
22

你也可以试试

db.Courses.Include("Modules.Chapters").Single(c => c.Id == id);
于 2015-05-05T11:13:50.733 回答
5

可以编写这样的扩展方法:

    /// <summary>
    /// Includes an array of navigation properties for the specified query 
    /// </summary>
    /// <typeparam name="T">The type of the entity</typeparam>
    /// <param name="query">The query to include navigation properties for that</param>
    /// <param name="navProperties">The array of navigation properties to include</param>
    /// <returns></returns>
    public static IQueryable<T> Include<T>(this IQueryable<T> query, params string[] navProperties)
        where T : class
    {
        foreach (var navProperty in navProperties)
            query = query.Include(navProperty);

        return query;
    }

即使在通用实现中也可以这样使用它:

string[] includedNavigationProperties = new string[] { "NavProp1.SubNavProp", "NavProp2" };

var query = context.Set<T>()
.Include(includedNavigationProperties);
于 2016-08-08T07:43:01.677 回答
2

这是我的项目

 var saleHeadBranch = await _context.SaleHeadBranch
 .Include(d => d.SaleDetailBranch)
 .ThenInclude(d => d.Item)
 .Where(d => d.BranchId == loginTkn.branchId)
 .FirstOrDefaultAsync(d => d.Id == id);
于 2020-10-03T06:31:49.550 回答