2009-09-29 64 views
6

我想對一組用戶進行排序。我可以訪問排序屬性和方向(asc,desc)。我目前的查詢順序如下。但是,正如你所看到的,它不能解釋排序方向。如何構建這個表達式而無需使用Dynamic Linq,或者爲「asc」或「desc」排序方向添加另一組語句。Linq從字符串排序方向

public override IQueryable<DalLinq.User> GetSort(IQueryable<DalLinq.User> query) 
{ 
    //SelectArgs.SortDirection <- Sort Direction 
    switch (SelectArgs.SortProperty) 
    { 
     case "LastName": 
     query = query.OrderBy(p => p.LastName); 
     break; 
     case "FirstName": 
     query = query.OrderBy(p => p.FirstName); 
     break; 
     default: 
     query = query.OrderBy(p => p.UserName); 
     break; 
    } 

    return query; 
} 

回答

12

理想情況下,想用OrderByDescending - 你當然可以作弊:

public static class MyExtensionMethods 
{ 
    public static IOrderedQueryable<TSource> OrderBy<TSource,TValue>(
     this IQueryable<TSource> source, 
     Expression<Func<TSource,TValue>> selector, 
     bool asc) 
    { 
     return asc ? source.OrderBy(selector) : source.OrderByDescending(selector); 
    } 
} 

並用OrderBy傳遞在選擇器和布爾?

如果你不需要靜態類型,你也可以從頭開始動態地構建表達式,如this short sample(類似於動態LINQ庫)。

+0

更好回答:) – Lazarus 2009-09-29 15:28:33

+1

很好,謝謝。我改變了布爾使用System.ComponentModel.ListSortDirection – zzz 2009-09-29 16:06:58

+0

'IBindingList' /'IBindingListView',然後? – 2009-09-29 19:47:13

2

這將會是一個if語句我想,沒有其他簡單的方法來做到這一點,即:

query = (SelectArgs.SortDirection == "asc") ? query.OrderBy(p => p.LastName) 
      : query.OrderByDescending(p => p.LastName); 

在有這樣的外觀以及:Sorting a list using Lambda/Linq to objects

0

看看CS代碼示例。有一個動態的Linq例子。

從樣本:

Northwind db = new Northwind(connString); 
db.Log = Console.Out; 

var query = 
    db.Customers.Where("City == @0 and Orders.Count >= @1", "London", 10). 
    OrderBy("CompanyName"). 
    Select("New(CompanyName as Name, Phone)"); 

訂購代碼:

public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string ordering, params object[] values) { 
     return (IQueryable<T>)OrderBy((IQueryable)source, ordering, values); 
    } 

    public static IQueryable OrderBy(this IQueryable source, string ordering, params object[] values) { 
     if (source == null) throw new ArgumentNullException("source"); 
     if (ordering == null) throw new ArgumentNullException("ordering"); 
     ParameterExpression[] parameters = new ParameterExpression[] { 
      Expression.Parameter(source.ElementType, "") }; 
     ExpressionParser parser = new ExpressionParser(parameters, ordering, values); 
     IEnumerable<DynamicOrdering> orderings = parser.ParseOrdering(); 
     Expression queryExpr = source.Expression; 
     string methodAsc = "OrderBy"; 
     string methodDesc = "OrderByDescending"; 
     foreach (DynamicOrdering o in orderings) { 
      queryExpr = Expression.Call(
       typeof(Queryable), o.Ascending ? methodAsc : methodDesc, 
       new Type[] { source.ElementType, o.Selector.Type }, 
       queryExpr, Expression.Quote(Expression.Lambda(o.Selector, parameters))); 
      methodAsc = "ThenBy"; 
      methodDesc = "ThenByDescending"; 
     } 
     return source.Provider.CreateQuery(queryExpr); 
    } 

不過可以肯定的是,你檢查用戶輸入的!