C# 实体框架获取具有自排序子集合的实体
我有一个实体(SystemUnit),其中包含子实体(角色)的集合:C# 实体框架获取具有自排序子集合的实体,c#,linq,entity-framework,C#,Linq,Entity Framework,我有一个实体(SystemUnit),其中包含子实体(角色)的集合: 公共部分类系统单元 { 公共系统股() { this.Roles=new HashSet(); this.Childs=newhashset(); } 公共int Id{get;set;} 公共可为空的ParentId{get;set;} public int SystemUnitTypeId{get;set;} 公共字符串名称{get;set;} 公共虚拟SystemUnitType SystemUnitType{get;s
公共部分类系统单元
{
公共系统股()
{
this.Roles=new HashSet();
this.Childs=newhashset();
}
公共int Id{get;set;}
公共可为空的ParentId{get;set;}
public int SystemUnitTypeId{get;set;}
公共字符串名称{get;set;}
公共虚拟SystemUnitType SystemUnitType{get;set;}
公共虚拟ICollection角色{get;set;}
公共虚拟ICollection子项{get;set;}
公共虚拟系统单元父级{get;set;}
}
我需要使用实体框架来获取所有系统单元,按Id Asc和包含的角色排序,按linq中的Id desc.Newbee排序(为此,您首先需要一个上下文实体,然后将您的系统单元实体作为对象添加到其中。例如:
public class entityContext{
public DbSet<SystemUnit> SystemUnit { get; set;}
}
公共类entityContext{
公共数据库集系统单元{get;set;}
}
然后在需要调用实体的方法中,写入:
entityContext ec = new entityContext();
List<SystemUnit> systemUnit = (from su in ec.SystemUnit .Include("Roles") orderby su.Id Asc).ToList();
entityContext ec=新的entityContext();
List systemUnit=(从ec.systemUnit.Include(“角色”)中的su按su.Id Asc.ToList()排序);
根据SystemUnit对象包含的角色。如果SystemUnit对象的Id由desc排序,那么在dec之前无法对角色进行排序。他们将根据SystemUnit对象检索可能重复的感谢,例如,M,但我如何包含按Id desc排序的角色?我在这里找到答案
entityContext ec = new entityContext();
List<SystemUnit> systemUnit = (from su in ec.SystemUnit .Include("Roles") orderby su.Id Asc).ToList();