[英]EF5 automatically sort navigation properties
I have a class structure which is used by Entity Framework (code-first); 我有一个Entity Framework使用的类结构(代码优先);
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Image> Images { get; set; }
}
public class Image
{
public int Id { get; set; }
public int Order { get; set; }
}
The Image
class has an Order
field, which can essentially handle sorting. Image
类有一个Order
字段,它实际上可以处理排序。 At some point in my application I write a line such as; 在我的应用程序的某些时候,我写了一行如:
BindImages(product.Images);
What I would like is for the Images
to be automatically sorted by the Order
field when I request a product.Images
, without me having to write product.Images.OrderBy(x => x.Order)
everywhere. 我想要的是,当我请求
product.Images
, Images
会自动按照Order
字段进行排序。我不需要在任何地方编写product.Images.OrderBy(x => x.Order)
。
Does anyone have any suggestions? 有没有人有什么建议?
EF cannot do for you what you want. EF不能为你做你想做的事。 But you can do this.
但你可以做到这一点。 You can add to your type another property
您可以向您的类型添加另一个属性
public IEnumerable<Image> OrderedImages
{
get{return Images.OrderBy(x => x.Order); }
}
and use it instead previous collection. 并使用它代替以前的集合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.