Entity framework .NET Framework 3.5 EntityCollection按问题排序

Entity framework .NET Framework 3.5 EntityCollection按问题排序,entity-framework,sql-order-by,Entity Framework,Sql Order By,我将.NET Framework 3.5与ASP.NET MVC结合使用,并将Linq用于数据对象的实体 我有一个升级对象,它与Location对象有一对多的关系 我要做的是从升级对象获取位置集合,并对它们进行排序,而不将它们放入另一个变量中。类似于 promotionEntity.Locations.OrderBy(l => l.Distance); 但它对*.Locations集合没有任何作用。有没有一种方法可以让EntityCollection按照我的需要进行排序,而无需将其放入另

我将.NET Framework 3.5与ASP.NET MVC结合使用,并将Linq用于数据对象的实体

我有一个升级对象,它与Location对象有一对多的关系

我要做的是从升级对象获取位置集合,并对它们进行排序,而不将它们放入另一个变量中。类似于

promotionEntity.Locations.OrderBy(l => l.Distance);
但它对*.Locations集合没有任何作用。有没有一种方法可以让EntityCollection按照我的需要进行排序,而无需将其放入另一个列表或变量中


谢谢。

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

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

如果类型类似于
List
,则可能需要创建一个新列表:

promotionEntity.Locations=
新列表(promotionEntity.Locations.OrderBy(l=>l.Distance));

如果位置类型为IEnumerable,则只需分配OrderBy的结果:

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

如果类型类似于
List
,则可能需要创建一个新列表:

promotionEntity.Locations=
新列表(promotionEntity.Locations.OrderBy(l=>l.Distance));

一种方法是使用:

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

var promotionEntity = context.Promotions.Select(p => new 
{
    p,
    Locations = p.Locations.OrderBy(l => l.Distance)
}).First();
一种方法是使用:

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

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

我将使用
promotionEntity.Locations=promotionEntity.Locations.OrderBy(l=>l.Distance.ToList()在第二种情况下。这是我正在尝试做的。promotionEntity.Locations被键入为EntityCollection,*.OrderBy使其成为System.Linq.IORDeredenumber,并且不会自动强制转换。它说确实存在显式转换。我只是还没弄明白。我会选择
promotionEntity.Locations=promotionEntity.Locations.OrderBy(l=>l.Distance.ToList()在第二种情况下。这是我正在尝试做的。promotionEntity.Locations被键入为EntityCollection,*.OrderBy使其成为System.Linq.IORDeredenumber,并且不会自动强制转换。它说确实存在显式转换。我只是还没弄明白。