Question

For example, with the following classes

public class Child
{
    public Guid Id { get; set; }
    public String Description { get; set; }
    public double Value { get; set; }

    public Child()
    {
        Id = Guid.NewGuid();
    }
}

public class Parent
{
    public Guid Id { get; set; }
    public String Name { get; set; }
    public virtual IList<Child> Children { get; set; }

    public Parent()
    {
        Id = Guid.NewGuid();
        Children = new List<Child>();
    }
}    

And context

public class TempContext : DbContext
{
    public DbSet<Child> Children { get; set; }
    public DbSet<Parent> Parents { get; set; }
}

How could I ensure that the objects in Parent.Children are ordered by Value

        TempContext tc = new TempContext();            
        var parents = tc.Parents.ToList();

        foreach (var p in parents)
        {
            Debug.WriteLine("Parent : {0}", (object) p.Name);
            foreach (var c in p.Children)
            {
                Debug.WriteLine("Child : {0} - {1}", c.Value, c.Description);
            }
            Debug.WriteLine("");
        }

Obviously, I can sort p.Children above before iterating the collection but I'd like the collection to already be ordered.

Was it helpful?

Solution

You must write a query for that. Ordering is not managed by mapping.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top