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