简体   繁体   English

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

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

I have a entity(SystemUnit), which contains collection of sub entities(Roles): 我有一个实体(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; }
}

I need using entity framework to get all system units, ordered by Id Asc with included Roles, ordered by id desc. 我需要使用实体框架来获取所有系统单元,这些系统单元由ID Asc排序,包括角色,由ID desc排序。 Newbee in linq ( 新蜂在linq(

The Roles included according to SystemUnit objects. 根据SystemUnit对象包含的角色。 If SystemUnit object has Id ordered by desc so in this way roles can not be orderby dec. 如果SystemUnit对象的ID由desc排序,则以这种方式不能按dec排序角色。 They will retrieve according to SystemUnit objects 他们将根据SystemUnit对象进行检索

For this you need a context entity first and then add your system unit entity as an object in it. 为此,您首先需要一个上下文实体,然后在其中添加系统单元实体作为对象。 For example: 例如:

public class entityContext{

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

}

And then in your method where you need to call the entity, write: 然后在需要调用实体的方法中,编写:

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