2010-04-24 120 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

相關問題