C#不同的型号订购

时间:2017-06-01 08:55:08

标签: c# .net api

我有3种不同的模型,共有4个属性。模型没有以任何方式连接,也没有扩展或实现任何东西。我有Angular 4 front,它发送一个列的字符串,通过它来进行排序。

现在我的方法有效,但必须有一个更聪明,更短的方法:

    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;
    }

我的第一个想法是某种方式&#34;翻译&#34;字符串到列名,但我读到这是一个坏主意,我应该使用强类型语言。为了使这项工作,我能做一些lambda吗?可能只有一个字符串发送2,其他值将是asc / desc?

3 个答案:

答案 0 :(得分:1)

我没有看到问题,你可以缩短它..

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

如果他们是linqToSql查询你可以做.OrderBy(“ColumnName DESC”)

答案 1 :(得分: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
    }
}

答案 2 :(得分: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)