【问题标题】:LINQ Include a child array of selectLINQ 包含 select 的子数组
【发布时间】:2020-03-01 20:52:41
【问题描述】:

我有以下运行良好的 LINQ 查询:

public IList<Course> GetEmployeeCourses(int id) {
  var employeeCourses = Context.Employees
    .Where(e => e.Id == id)
    .SelectMany(e => e.employeeCourses.Select(ec => ec.Course))
    .ToList();

  return employeeCourses;
}

问题是,我现在还需要在返回 Course 数组时包含所选课程的子数组 (Urls)。

类似:

public IList<Course> GetEmployeeCourses(int id) {
  var employeeCourses = Context.Employees
    .Where(e => e.Id == id)
    .SelectMany(e => e.employeeCourses.Select(ec => ec.Course))
    .Include(c => c.Urls)
    .ToList();

  return employeeCourses;
}

这样返回 JSON:(尽管 Course 模型有超过 15 个属性)

[
  {
     "Name": "Course1",
     "Type": "Maths",
     "Location": "Block C",
     "Urls": [
          {
            "Link": "https://url1forcourse1.com"
          },
          {
            "Link": "https://url2forcourse1.com"
          }
      ]
  }
  {
     "Name": "Course2"
     "Type": "Computer Science",
     "Location": "Block A",
     "Urls": [
          {
            "Link": "https://url1forcourse2.com"
          },
          {
            "Link": "https://url2forcourse2.com"
          }
      ]
  },
  {
     "Name": "Course3"
     "Type": "The Art of Dish Washing",
     "Location": "Block E",
     "Urls": [
          {
            "Link": "https://url1forcourse3.com"
          },
          {
            "Link": "https://url2forcourse3.com"
          }
      ]
  }
]

如何以最有效的方式实现这一目标?目前我似乎根本无法获取名为“Urls”的子数组,它始终为空。

另外,我正在使用 Fluent API 和这个 EmployeeCourse 配置:

ToTable("EmployeeCourses");

HasKey(q => new { q.CourseId, q.Employee.Id})

HasRequired(x => x.Employee)
   .WithMany(x => x.EmployeeCourses)
   .HasForeignKey(x => x.CourseId);

HasRequired(x => x.Course)
   .WithMany(x => x.EmployeeCourses)
   .HasForeignKey(x => x.EmployeeId);

【问题讨论】:

  • 如果这是EF6EF Core?请具体并相应标记。

标签: c# linq entity-framework-6


【解决方案1】:

来自Eagerly loading multiple levels

预加载是查询一种类型的实体的过程 还加载相关实体作为查询的一部分。急切加载是 使用 Include 方法实现。

你可以这样尝试,因此它可以让你加载所有EmployeeCourses和相关的Courses

public IList<Course> GetEmployeeCourses(int id) {
  var employeeCourses = Context.Employees
    .Where(e => e.Id == id)
    .Include(e => e.employeeCourses.Select(ec => ec.Course))
    .SelectMany(e => e.employeeCourses.Select(ec => ec.Course))
    .ToList();

  return employeeCourses;
}

【讨论】:

  • 这在什么时候包含 URL?它们是 Course 的子数组。
  • 据我了解UrlsCourse类的属性,对吗?如果是这样,当您包含Course 时,您可以轻松获得Urls
  • 如何轻松获取网址?
【解决方案2】:

您可能喜欢使用链式SelectMany 作为:

public IList<Course> GetEmployeeCourses(int id)
{
  var employeeCourses = Context.Employees
                        .Where(e => e.Id == id)
                        .SelectMany(e => e.employeeCourses
                        .SelectMany(ec => ec.Course))
                        .Include(c => c.Urls)
                        .ToList();



    return employeeCourses;
}

【讨论】:

  • 这会获取 URL,但如果每个课程中包含嵌套子 URL 的课程,我需要一个列表
  • @DBoi 看看。它现在应该可以工作了。它将提供NameUrls
  • 嗨 MKR,感谢您帮助我。虽然上面的示例只有课程的“名称”和“网址”属性,但我的应用程序有许多课程属性,包括“名称”、“类型”、“位置”等(超过 15 个属性)。有没有办法获取 Course 模型中的所有道具以及 Urls 子数组?而不是单独获取它们。
  • @DBoi 我已经更新了我的答案。看一看。它应该可以工作。
猜你喜欢
  • 1970-01-01
  • 2016-03-13
  • 1970-01-01
  • 2017-01-21
  • 1970-01-01
  • 2021-10-27
  • 1970-01-01
  • 2022-01-21
  • 2020-04-17
相关资源
最近更新 更多