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
Linq 当我们有一个自关系实体时,如何编写一个查询来获取每个实体和子实体_Linq_Linq To Sql_Linq To Entities_Ef Core 2.2 - Fatal编程技术网

Linq 当我们有一个自关系实体时,如何编写一个查询来获取每个实体和子实体

Linq 当我们有一个自关系实体时,如何编写一个查询来获取每个实体和子实体,linq,linq-to-sql,linq-to-entities,ef-core-2.2,Linq,Linq To Sql,Linq To Entities,Ef Core 2.2,我使用Ef Core 2.2,我有一个实体,它有自己的关系,每个实体可能有一个实体列表 这是我的实体 public class CourseGroup { public int ID { get; set; } public string Title { get; set; } public bool IsDeleted { get; set; } //Foreign key public int?

我使用Ef Core 2.2,我有一个实体,它有自己的关系,每个实体可能有一个实体列表

这是我的实体

   public class CourseGroup
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public bool IsDeleted { get; set; }

        //Foreign key
        public int? ParentId { get; set; }

        //Navigations Property
        public CourseGroup ParentCourseGroup { get; set; }

        //Relatons => Self Relation
        public ICollection<CourseGroup> Groups { get; set; }
    }
公共课程组
{
公共int ID{get;set;}
公共字符串标题{get;set;}
公共布尔被删除{get;set;}
//外键
public int?ParentId{get;set;}
//导航属性
公共课程组父课程组{get;set;}
//关系=>自我关系
公共ICollection组{get;set;}
}
这是我的配置

 class CourseGroupConfig : IEntityTypeConfiguration<CourseGroup>
    {
        public void Configure(Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder<CourseGroup> builder)
        {
            builder.HasKey(c => c.Id);
            builder.Property(c => c.Id).ValueGeneratedOnAdd();
            builder.Property(c => c.Title).HasMaxLength(60);

            //Relations
            builder.HasOne(c => c.ParentCourseGroup).WithMany(c => c.Groups).HasForeignKey(c => c.ParentId);
        }
    }
class CourseGroupConfig:IEntityTypeConfiguration
{
公共void配置(Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder)
{
HasKey(c=>c.Id);
属性(c=>c.Id).ValueGeneratedOnAdd();
builder.Property(c=>c.Title).HasMaxLength(60);
//关系
HasOne(c=>c.ParentCourseGroup).WithMany(c=>c.Groups).HasForeignKey(c=>c.ParentId);
}
}
我想得到所有实体和子实体。如何使用Linq编写此查询?
请帮我写这个查询

db.Set().ToList()
?请参阅或
db.Set().Include(cg=>cg.Groups.ToList())
?否。查询结果必须是这样的。每个实体可能都有一个实体列表1.WebProgramming.AspCore.NodeJs 2.MobileProgramming.React naitive.xamarin那么这些代码片段没有给您提供您需要的东西是什么呢?首先,我没有看到您的评论。我应该试试看它是否有效。感谢您的回答
db.Set().ToList()
?请参阅或
db.Set().Include(cg=>cg.Groups.ToList())
?否。查询结果必须是这样的。每个实体可能都有一个实体列表1.WebProgramming.AspCore.NodeJs 2.MobileProgramming.React naitive.xamarin那么这些代码片段没有给您提供您需要的东西是什么呢?首先,我没有看到您的评论。我应该试试看它是否有效。谢谢你的回答
   public class CourseGroup
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public bool IsDeleted { get; set; }

        //Foreign key
        public int? ParentId { get; set; }

        //Navigations Property
        public CourseGroup ParentCourseGroup { get; set; }

        //Relatons => Self Relation
        public ICollection<CourseGroup> Groups { get; set; }
    }