我可以使用 Entity Framework Code First 指定子实体默认排序吗?
例如,对于以下类
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>();
}
}
和上下文,
public class TempContext : DbContext
{
public DbSet<Child> Children { get; set; }
public DbSet<Parent> Parents { get; set; }
}
我如何确保 Parent.Children 中的对象按 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("");
}
显然,我可以在迭代集合之前对上面的 p.Children 进行排序,但我希望集合已经排序。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须编写查询< /a> 为此。排序不由映射管理。
You must write a query for that. Ordering is not managed by mapping.