1st approach: (only suitable for LINQ for objects with responsibility defined as IList)
You can copy Employees to the new collection, but that means you need to copy each field to a new employee, something like this:
var employees = from e in _context.Employees orderby e.Name select new Employee { Name = e.Name, ...re-assign other properties... Responsibilities = e.Responsibilities.OrderBy(r => r.Name).ToList() };
Second approach:
Using DataLoadOptions . Basically, you create a DataLoadOptions object that indicates how to arrange a specific relationship, so in your case:
var options = new DataLoadOptions(); options.AssociateWith<Employees>(e => e.Responsibilities.OrderBy(r => r.Name)); _context.LoadOptions = options; var employees = _context.Employees.OrderBy(e => e.Name);
Duties will be ordered automatically.
Denis ivin
source share