Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ包含select的子数组_C#_Linq_Entity Framework 6 - Fatal编程技术网

C# LINQ包含select的子数组

C# LINQ包含select的子数组,c#,linq,entity-framework-6,C#,Linq,Entity Framework 6,我有以下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 employeeC

我有以下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;
}
我如何以最有效的方式实现这一点?我现在根本无法得到名为“URL”的子数组,它总是空的

此外,我正在将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);
发件人:

即时加载是查询一种类型的实体的过程 还将加载相关实体作为查询的一部分。急切的装载是必要的 通过使用Include方法实现

您可以这样尝试,因此它允许您加载所有
员工课程
和相关
课程

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;
}
public IList GetEmployeeCourses(int-id){
var employeeCourses=Context.Employees
.其中(e=>e.Id==Id)
.包括(e=>e.employeeCourses.Select(ec=>ec.Course))
.SelectMany(e=>e.employeeCourses.Select(ec=>ec.courses))
.ToList();
返回员工培训课程;
}

您可以使用chained
选择many
作为:

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;
}
public IList GetEmployeeCourses(int-id)
{
var employeeCourses=Context.Employees
.其中(e=>e.Id==Id)
.选择许多(e=>e.employeeCourses)
.SelectMany(ec=>ec.Course))
.Include(c=>c.url)
.ToList();
返回员工培训课程;
}

这在哪一点包括URL?它们当然是一个子数组。据我所知,
url
Course
类的属性,对吗?如果是这样,那么当您包括
课程
时,您可以轻松获取
URL
如何轻松获取URL?如果是
EF6
EF核心
?请具体一点,并相应地标记。这会得到URL,但如果每个课程中都有嵌套的子URL,我需要一个列表Course@DBoi看一看。现在应该可以了。它将提供
名称
URL
。你好,MKR,谢谢你的帮助。虽然上面的例子只有课程的“Name”和“url”属性,但我的应用程序有很多课程属性,包括“Name”、“Type”、“Location”等(超过15个属性)。有没有办法获取课程模型中的所有道具以及URL子数组?而不是单独获取它们。@DBoi我已经更新了我的答案。看一看。它应该会起作用。
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;
}
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;
}