2010-04-24 116 views
0

我有一个LINQ查询,填充设计师列表。如何使用LINQ对列表进行排序?

由于我使用下面的过滤器,我的排序不能按照我希望的方式运行。

我的问题是,鉴于下面的代码,我怎么能最好的排序这个列表后事实或排序查询?

我试图排序使用下面的脚本在事后的名单,但我收到一个编译器错误:

List<TBLDESIGNER> designers = new List<TBLDESIGNER>(); 
designers = 'calls my procedure below and comes back with an unsorted list of designers' 
designers.Sort((x, y) => string.Compare(x.FIRST_NAME, y.LAST_NAME)); 

我的查询如下:

List<TBLDESIGNER> designer = null; 

using (SOAE strikeOffContext = new SOAE()) 
{ 
    //Invoke the query 
    designer = AdminDelegates.selectDesignerDesigns.Invoke(strikeOffContext).ByActive(active).ByAdmin(admin).ToList(); 
} 

代表:

public static Func<SOAE, IQueryable<TBLDESIGNER>> selectDesignerDesigns = 
     CompiledQuery.Compile<SOAE, IQueryable<TBLDESIGNER>>(
     (designer) => from c in designer.TBLDESIGNER.Include("TBLDESIGN") 
         orderby c.FIRST_NAME ascending 
         select c); 

Filter ByActive:

public static IQueryable<TBLDESIGNER> ByActive(this IQueryable<TBLDESIGNER> qry, bool active) 
    { 
     //Return the filtered IQueryable object 
     return from c in qry 
       where c.ACTIVE == active 
       select c; 

    } 

过滤ByAdmin:

public static IQueryable<TBLDESIGNER> ByAdmin(this IQueryable<TBLDESIGNER> qry, bool admin) 
{ 
    //Return the filtered IQueryable object 
    return from c in qry 
      where c.SITE_ADMIN == admin 
      select c; 

} 

由于提前, 比利

+0

您是否必须为您的数据库表命名为TBLDESIGNER? Ooph。如果你必须保持这一点,我感到很难过。 :-) – 2010-04-24 12:33:33

+0

只是我们使用的命名约定。你为什么这么说? – 2010-04-24 12:38:46

+0

有更好的模式吗?我想听听它。 – 2010-04-24 12:52:27

回答

3

有很多事情在您的文章,但我选择回答被问到的问题:

Linq - How to sort a list

使用声明性的OrderBy和ThenBy方法。

designers = designers 
    .OrderBy(td => td.FirstName) 
    .ThenBy(td => td.LastName) 
    .ToList(); 
+0

谢谢戴夫。这正是我最终使用的。 – 2010-04-24 12:46:52

相关问题