【问题标题】:entity framework get entities with child collection with self order实体框架通过自序获取具有子集合的实体
【发布时间】:2014-11-28 06:07:22
【问题描述】:

我有一个实体(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 中的 Newbee (

【问题讨论】:

标签: c# linq entity-framework


【解决方案1】:

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

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();

【讨论】:

  • 感谢您的示例,但我如何才能包含按 id desc 排序的角色?
【解决方案2】:

根据 SystemUnit 对象包含的角色。如果 SystemUnit 对象的 Id 由 desc 排序,那么这样角色就不能按 dec 排序。他们会根据 SystemUnit 对象进行检索

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-11
  • 2022-01-11
  • 2016-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多