EF4 LINQ订购父母和所有有Eager载入的子集合(.Include()) [英] EF4 LINQ Ordering Parent and all child collections with Eager Loading (.Include())

查看:126
本文介绍了EF4 LINQ订购父母和所有有Eager载入的子集合(.Include())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在网格上进行分层数据绑定,我需要让数据库服务器对我的对象执行排序。我可以很容易地排列父集合,但我似乎无法弄清楚如何对所有的子集合进行排序。我有一个模型,其子集合嵌套在3深,所有这些集合都需要排序。



以下是我正在尝试完成的示例模型:

  public class Year 
{
public int Id {get;组; }
public string Name {get;组; }
public List< Make>使{get;组; }
}
public class Make
{
public int Id {get;组; }
public string Name {get;组; }
public List< Model>模型{get;组;
}
public class Model
{
public int Id {get;组; }
public string Name {get;组; }
public List< Color>颜色{get;组; }
}
public class Color
{
public int Id {get;组; }
public string Name {get;组;我试图加载一个年对象列表。
$



<这有一个Makes的集合,它有一个包含颜色集合的模型集合。我需要根据其名称属性对所有这些对象进行排序。



我已经尝试这样做:

 列表<年> year = db.Years.OrderBy(it.Name)
.Include(Makes)。OrderBy(it.Name)
.Include(Makes.Models)。OrderBy it.Name)
.Include(Makes.Models.Colors)。OrderBy(it.Name)
.ToList();

但它。只是从...中选择...的表的别。有没有办法为子表创建别名,所以我可以在单个查询中对它们执行排序?

解决方案

如果您需要在内部导航属性(例如模型)上进行排序或过滤,那么您无法再使用包含方法加载它们。相反,
您可以使用 EntityCollection< TEntity> .CreateSourceQuery方法 ,如下所示:

 列表年份= db.Years.OrderBy ( it.Name)ToList(); 
foreach(年份)
{
var makesQuery = year.Makes.CreateSourceQuery()。OrderBy(m => m.Name);
year.Makes.Attach(makesQuery);

foreach(make in year.Makes)
{
var modelsQuery = make.Models.CreateSourceQuery()。OrderBy(m => m.Name);
make.Models.Attach(modelsQuery);

foreach(在Make.Models中的模型)
{
var colQuery = model.Colors.CreateSourceQuery()。OrderBy(c => c.Name);
model.Models.Attach(colQuery);
}
}
}

这样,将构建其导航属性的所有命令。


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?

解决方案

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:

List years = db.Years.OrderBy("it.Name").ToList();
foreach(year in years) 
{
    var makesQuery = year.Makes.CreateSourceQuery().OrderBy(m => m.Name);
    year.Makes.Attach(makesQuery);  

    foreach(make in year.Makes) 
    {
        var modelsQuery = make.Models.CreateSourceQuery().OrderBy(m => m.Name);
        make.Models.Attach(modelsQuery);

        foreach(model in make.Models) 
        {
            var colQuery = model.Colors.CreateSourceQuery().OrderBy(c => c.Name);
            model.Models.Attach(colQuery);        
        }
    }
}

This way, the years object will be constructed with having all of its navigation properties ordered.

这篇关于EF4 LINQ订购父母和所有有Eager载入的子集合(.Include())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆