0

I have a entity(SystemUnit), which contains collection of sub entities(Roles):

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. Newbee in linq (

Yuriy Mayorov
  • 961
  • 4
  • 15
  • 32
  • 1
    possible duplicate of [Entity Framework loading child collection with sort order](http://stackoverflow.com/questions/9938956/entity-framework-loading-child-collection-with-sort-order) – Dennis Oct 03 '14 at 07:16

2 Answers2

1

The Roles included according to SystemUnit objects. If SystemUnit object has Id ordered by desc so in this way roles can not be orderby dec. They will retrieve according to SystemUnit objects

  • http://stackoverflow.com/questions/9938956/entity-framework-loading-child-collection-with-sort-order I found answer here – Yuriy Mayorov Oct 03 '14 at 07:38
0

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();
Nissa
  • 4,636
  • 8
  • 29
  • 37