问题
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.
回答1:
You must write a query for that. Ordering is not managed by mapping.
来源:https://stackoverflow.com/questions/4741714/can-i-specify-child-entity-default-ordering-with-entity-framework-code-first