繁体   English   中英

实体框架通过自身顺序获取带有子集合的实体

[英]entity framework get entities with child collection with self order

我有一个实体(SystemUnit),其中包含子实体(角色)的集合:

public partial class SystemUnit
{
    public SystemUnit()
    {
        this.Roles = new HashSet<Role>();
        this.Childs = new HashSet<SystemUnit>();
    }

    public int Id { get; set; }
    public Nullable<int> ParentId { get; set; }
    public int SystemUnitTypeId { get; set; }
    public string Name { get; set; }

    public virtual SystemUnitType SystemUnitType { get; set; }
    public virtual ICollection<Role> Roles { get; set; }
    public virtual ICollection<SystemUnit> Childs { get; set; }
    public virtual SystemUnit Parent { get; set; }
}

我需要使用实体框架来获取所有系统单元,这些系统单元由ID Asc排序,包括角色,由ID desc排序。 新蜂在linq(

根据SystemUnit对象包含的角色。 如果SystemUnit对象的ID由desc排序,则以这种方式不能按dec排序角色。 他们将根据SystemUnit对象进行检索

为此,您首先需要一个上下文实体,然后在其中添加系统单元实体作为对象。 例如:

public class entityContext{

    public DbSet<SystemUnit> SystemUnit { get; set;}

}

然后在需要调用实体的方法中,编写:

entityContext ec = new entityContext();
 List<SystemUnit> systemUnit = (from su in ec.SystemUnit .Include("Roles") orderby su.Id Asc).ToList();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM