.NET Framework 3.5 EntityCollection OrderBy 问题

发布于 2024-10-01 03:22:56 字数 352 浏览 0 评论 0原文

我将 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少年亿悲伤 2024-10-08 03:22:56

如果位置类型是 IEnumerable,您可以只分配 OrderBy 的结果:

promotionEntity.Locations = PromotionEntity.Locations.OrderBy(l => l.Distance)

如果类型类似于 List 那么您可能必须创建一个新列表:

promotionEntity.Locations =
new List(promotionEntity.Locations.OrderBy(l => l.Distance));

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));

得不到的就毁灭 2024-10-08 03:22:56

一种方法是使用 EntityCollection.CreateSourceQuery 方法

var promotionEntity = context.Promotions.First();
var sq = product.Locations.CreateSourceQuery().OrderBy(l => l.Distance);
promotionEntity.Locations.Attach(sq);

或者,如果您不关心结果对象的类型,可以在匿名类型内进行投影:

var promotionEntity = context.Promotions.Select(p => new 
{
    p,
    Locations = p.Locations.OrderBy(l => l.Distance)
}).First();

One way is to use EntityCollection.CreateSourceQuery Method:

var promotionEntity = context.Promotions.First();
var sq = product.Locations.CreateSourceQuery().OrderBy(l => l.Distance);
promotionEntity.Locations.Attach(sq);

Or if you don't care about the resultant object's type can do projection inside an anonymous type:

var promotionEntity = context.Promotions.Select(p => new 
{
    p,
    Locations = p.Locations.OrderBy(l => l.Distance)
}).First();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文