.NET Framework 3.5 EntityCollection OrderBy 问题
我将 .NET Framework 3.5 与 ASP.NET MVC 结合使用,并将 Linq To Entity 用于我的数据对象。
我有一个与位置对象具有一对多关系的促销对象。
我想要做的是从 Promotion 对象中获取位置集合并对它们进行排序,而不将它们放入另一个变量中。类似...
promotionEntity.Locations.OrderBy(l => l.Distance);
但它对 *.Locations 集合没有任何作用。有没有一种方法可以让 EntityCollection 按我需要的方式排序,而无需将其放入另一个列表或变量中?
谢谢。
I'm using .NET Framework 3.5 with ASP.NET MVC and I'm using Linq To Entity for my data objects.
I have a Promotion Object that has a 1 to many relationship to the Location object.
What I want to do is take the collection of Locations from the Promotion object and sort those without putting them into another variable. Something like...
promotionEntity.Locations.OrderBy(l => l.Distance);
But it does nothing to the *.Locations collections. Is there a way that I can have that EntityCollection sorted how I need without putting it in another List or variable?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果位置类型是 IEnumerable,您可以只分配 OrderBy 的结果:
promotionEntity.Locations = PromotionEntity.Locations.OrderBy(l => l.Distance)
如果类型类似于
List
那么您可能必须创建一个新列表:promotionEntity.Locations =(promotionEntity.Locations.OrderBy(l => l.Distance));
new List
If the type of locations is IEnumerable you can just assign the result of OrderBy:
promotionEntity.Locations = promotionEntity.Locations.OrderBy(l => l.Distance)
If the type is something like
List<Location>
then you will probably have to create a new List:promotionEntity.Locations =
new List<Location>(promotionEntity.Locations.OrderBy(l => l.Distance));
一种方法是使用 EntityCollection.CreateSourceQuery 方法:
或者,如果您不关心结果对象的类型,可以在匿名类型内进行投影:
One way is to use EntityCollection.CreateSourceQuery Method:
Or if you don't care about the resultant object's type can do projection inside an anonymous type: