2017-06-01 72 views
1

我有3個不同的型號共享約4個屬性。模型沒有以任何方式連接,也沒有擴展或實現任何東西。我有一個Angular 4 front,它發送一個字符串,按順序發送。C#不同型號訂購

現在我這裏有方法可行,但必須有做一個更聰明和更短的方式:

public static IEnumerable<ApiResponseTime> orderBy(ApiResponseTime apiResponseTime, IEnumerable<ApiResponseTime> returnList) 
    { 
     switch (apiResponseTime.order_by_value) 
     { 
      case "id": 
      returnList = returnList.OrderBy(x => x.id); 
      break; 

      case "!id": 
      returnList = returnList.OrderByDescending(x => x.id); 
      break; 

      case "tenant_id": 
      returnList = returnList.OrderBy(x => x.tenant_id); 
      break; 

      case "!tenant_id": 
      returnList = returnList.OrderByDescending(x => x.tenant_id); 
      break; 

      case "start_time": 
      returnList = returnList.OrderBy(x => x.start_time); 
      break; 

      case "!start_time": 
      returnList = returnList.OrderByDescending(x => x.start_time); 
      break; 

      case "total_time": 
      returnList = returnList.OrderBy(x => x.total_time); 
      break; 

      case "!total_time": 
      returnList = returnList.OrderByDescending(x => x.total_time); 
      break; 

      case "status_code": 
      returnList = returnList.OrderBy(x => x.status_code); 
      break; 

      case "!status_code": 
      returnList = returnList.OrderByDescending(x => x.status_code); 
      break; 

      case "api_endpoint_route": 
      returnList = returnList.OrderBy(x => x.api_endpoint_route); 
      break; 

      case "!api_endpoint_route": 
      returnList = returnList.OrderByDescending(x => x.api_endpoint_route); 
      break; 

      case "requesting_ip": 
      returnList = returnList.OrderBy(x => x.requesting_ip); 
      break; 

      case "!requesting_ip": 
      returnList = returnList.OrderByDescending(x => x.requesting_ip); 
      break; 

      default: 
      break; 
     } 

     return returnList; 
    } 

我的第一個想法是有點「翻譯」字符串,列名,但我讀了這是一個壞主意,我應該使用強類型語言。是否有一些lambda我可以做到這一點?可能相反,只有一個字符串發送2,其他值將是asc/desc?

+0

明顯縮短我看到的是設置一個布爾表示,是否存在「!」在'order_by_value'字符串中,修剪「!」從它並刪除所有的情況下有'case'!''然後,如果布爾值爲true,請將'returnList'列表反轉。 –

回答

1

我不明白的問題,可以通過縮短它..

... 
case "id" : return returnList.OrderBy(x => x.id); 
case "!id": return returnList.OrderByDescending(x => x.id); 
... 

如果他們linqToSql查詢,你可以做.OrderBy( 「的ColumnName DESC」)

+0

當然,但技術上不是我所需要的。成類似'returnList.OrderBy(X => x.WhereColumnName(串)' – Norgul

+1

如果他們linqToSql查詢,你可以做.OrderBy( 「的ColumnName DESC」) –

+0

這一點也適用'System.Linq.Dynamic.Core'包,雖然它沒有將「DESC」作爲參數轉發的選項,但是我可以用if-else來完成,只需修改答案,我會接受它 – Norgul

0

可能會有點更清潔使用兩個枚舉:字段和方向。

public enum Field 
{ 
    Id, 
    TenantId, 
    StartTime, 
    ... 
} 

public enum Direction 
{ 
    Ascending, 
    Descending, 
} 

public IEnumerable<T> Sort<T, KEY>(IEnumerable<T> l, Direction d, Func<T, KEY> getKey) 
{ 
    switch (d) 
    { 
     case Direction.Ascending: return l.OrderBy(getKey); 
     case Direction.Descending: return l.OrderByDescending(getKey); 
     default:     return l; 
    } 
} 

public IEnumerable<ApiResponseTime> orderBy(Field f, Direction d, IEnumerable<ApiResponseTime> l) 
{ 
    switch (f) 
    { 
     case Field.Id:   return Sort(l, d, x => x.id); 
     case Field.StartTime: return Sort(l, d, x => x.start_time); 

     // and so on 
    } 
} 
0

更好地構建orderby表達式,它將參數作爲字符串並按該屬性排序。這將刪除您的開關並使您的代碼更通用。

 
public static IQueryable OrderByProperty(this IQueryable source, string orderByPropertyName){ 
    ParameterExpression paramterExpression = Expression.Parameter(typeof (T)); 
    Expression orderByProperty = Expression.Property(paramterExpression, orderByPropertyName); 
    LambdaExpression lambda = Expression.Lambda(orderByProperty, paramterExpression); 
    MethodInfo genericMethod = OrderByMethod.MakeGenericMethod(typeof (T), orderByProperty.Type); 
    return (IQueryable)genericMethod.Invoke(null, new object[] {source, lambda});
}

現在刪除您的開關情況,並呼籲像
returnList.OrderByProperty(apiResponseTime.order_by_value)