EF4 LINQ 使用预加载 (.Ininclude()) 排序父集合和所有子集合
我正在网格上进行分层数据绑定,并且需要让数据库服务器对我的对象执行排序。我可以轻松地对父集合进行排序,但我似乎无法弄清楚如何对所有子集合进行排序。我有一个模型,其中子集合嵌套了 3 层,所有这些集合都需要进行排序。
这是我想要完成的示例模型:
public class Year
{
public int Id { get; set; }
public string Name { get; set; }
public List<Make> Makes { get; set; }
}
public class Make
{
public int Id { get; set; }
public string Name { get; set; }
public List<Model> Models { get; set; }
}
public class Model
{
public int Id { get; set; }
public string Name { get; set; }
public List<Color> Colors { get; set; }
}
public class Color
{
public int Id { get; set; }
public string Name { get; set; }
}
我正在尝试加载“Year”对象的列表。它有一个品牌集合,其中有一个模型集合,其中有一个颜色集合。我需要根据所有这些对象的名称属性对它们进行排序。
我曾尝试这样做:
List<Year> years = db.Years.OrderBy("it.Name")
.Include("Makes").OrderBy("it.Name")
.Include("Makes.Models").OrderBy("it.Name")
.Include("Makes.Models.Colors").OrderBy("it.Name")
.ToList();
但是“它”。只是从...中选择的表的别名,在本例中为“年”。有没有办法为子表创建别名,以便我也可以在单个查询中对它们执行排序?
I am doing hierarchical data binding on a grid, and I need to have the database server perform the sorting on my objects. I can easily sort the parent collection, but I can't seem to figure out how to also sort all the child collections. I have a model which has child collections nested 3 deep, and all of these collections need to be sorted.
Here is a sample model of what I'm trying to accomplish:
public class Year
{
public int Id { get; set; }
public string Name { get; set; }
public List<Make> Makes { get; set; }
}
public class Make
{
public int Id { get; set; }
public string Name { get; set; }
public List<Model> Models { get; set; }
}
public class Model
{
public int Id { get; set; }
public string Name { get; set; }
public List<Color> Colors { get; set; }
}
public class Color
{
public int Id { get; set; }
public string Name { get; set; }
}
I am trying to load a List of "Year" objects. This has a collection of Makes, which has a collection of Models which has a Collection of Colors. I need to sort all of these objects based on their name property.
I have tried doing this:
List<Year> years = db.Years.OrderBy("it.Name")
.Include("Makes").OrderBy("it.Name")
.Include("Makes.Models").OrderBy("it.Name")
.Include("Makes.Models.Colors").OrderBy("it.Name")
.ToList();
but "it." is only an alias for the table being selected from... in this case "Years". Is there any way to create an alias for the child tables so I can perform sorting on them as well in a single query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您需要对内部导航属性(例如模型)进行排序或过滤,那么您不能再使用 Include 方法预先加载它们。反而,.CreateSourceQuery 方法 像这样:
您可以使用 EntityCollection
这样,years 对象将被构造为具有所有导航属性的排序。
If you need to have Ordering or Filtering on inner navigation properties (e.g. Models) then you cannot eager load them using Include method anymore. Instead,
you can use EntityCollection<TEntity>.CreateSourceQuery Method like this:
This way, the years object will be constructed with having all of its navigation properties ordered.