C#不同型号的订购
我有3个不同的模型,它们共享大约4个属性。模型没有以任何方式连接,也没有扩展或实现任何东西。我有一个角度4前端,它发送一个列的字符串,通过它应该进行排序 现在,我这里的方法可行了,但必须有一种更聪明、更短的方法:C#不同型号的订购,c#,.net,api,C#,.net,Api,我有3个不同的模型,它们共享大约4个属性。模型没有以任何方式连接,也没有扩展或实现任何东西。我有一个角度4前端,它发送一个列的字符串,通过它应该进行排序 现在,我这里的方法可行了,但必须有一种更聪明、更短的方法: public static IEnumerable<ApiResponseTime> orderBy(ApiResponseTime apiResponseTime, IEnumerable<ApiResponseTime> returnList)
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;
}
公共静态IEnumerable orderBy(APIResponseTimeAPIResponseTimeIEnumerable返回列表)
{
开关(apiResponseTime.order_by_值)
{
案例“id”:
returnList=returnList.OrderBy(x=>x.id);
打破
案例“!id”:
returnList=returnList.OrderByDescending(x=>x.id);
打破
案例“租户id”:
returnList=returnList.OrderBy(x=>x.tenant\u id);
打破
案例“!租户id”:
returnList=returnList.OrderByDescending(x=>x.tenant\u id);
打破
案例“开始时间”:
returnList=returnList.OrderBy(x=>x.start\u时间);
打破
案例“!开始时间”:
returnList=returnList.OrderByDescending(x=>x.start\u时间);
打破
案例“总时间”:
returnList=returnList.OrderBy(x=>x.total_time);
打破
案例“!总时间”:
returnList=returnList.OrderByDescending(x=>x.total_time);
打破
案例“状态代码”:
returnList=returnList.OrderBy(x=>x.status\u代码);
打破
案例“!状态代码”:
returnList=returnList.OrderByDescents(x=>x.status\u代码);
打破
案例“api_端点_路由”:
returnList=returnList.OrderBy(x=>x.api\u endpoint\u route);
打破
案例“!api_端点_路由”:
returnList=returnList.OrderByDescending(x=>x.api\U端点\U路由);
打破
案例“请求知识产权”:
returnList=returnList.OrderBy(x=>x.ip);
打破
案例“!请求ip”:
returnList=returnList.OrderByDescending(x=>x.ip);
打破
违约:
打破
}
退货清单;
}
我的第一个想法是以某种方式将字符串“翻译”为列名,但我读到这是一个坏主意,我应该使用强类型语言。我能做点什么来让这一切顺利吗?可能只有一个字符串发送2,其他值为asc/desc?我看不出有问题,您可以将其缩短
...
case "id" : return returnList.OrderBy(x => x.id);
case "!id": return returnList.OrderByDescending(x => x.id);
...
如果它们是linqToSql查询,您可以执行。OrderBy(“ColumnName DESC”)使用两个枚举可能会更干净一些:字段和方向
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
}
}
公共枚举字段
{
身份证件
租户,
开始的时候,
...
}
公共枚举方向
{
提升
下降,
}
公共IEnumerable排序(IEnumerable l,方向d,函数getKey)
{
开关(d)
{
案例方向.升序:返回l.OrderBy(getKey);
案例方向.下行:返回l.OrderByDescending(getKey);
默认值:返回l;
}
}
公共IEnumerable orderBy(字段f,方向d,IEnumerable l)
{
开关(f)
{
case Field.Id:返回排序(l,d,x=>x.Id);
case Field.StartTime:返回排序(l,d,x=>x.start\u时间);
//等等
}
}
最好构建orderby表达式,该表达式将参数作为字符串并按该属性排序。这将删除开关并使代码更通用。
公共静态IQueryable OrderByProperty(此IQueryable源,字符串orderByPropertyName){
ParameterExpression ParameterExpression=Expression.Parameter(typeof(T));
Expression orderByProperty=Expression.Property(parametexpression,orderByPropertyName);
LambdaExpression lambda=Expression.lambda(orderByProperty,ParameterExpression);
MethodInfo genericMethod=OrderByMethod.MakeGenericMethod(typeof(T),orderByProperty.Type);
return(IQueryable)genericMethod.Invoke(null,新对象[]{source,lambda});
}
现在,卸下开关盒,像returnList.OrderByProperty(apResponseTime.order\u by\u value)一样调用它。当然,但从技术上讲,这不是我需要的。我在研究类似于
returnList.OrderBy(x=>x.WhereColumnName(string)
的东西,如果它们是linqToSql查询,您可以执行。OrderBy(“ColumnName DESC”)这适用于System.Linq.Dynamic.Core
包,尽管它没有转发“DESC”的选项作为一个参数,但我可以用if else来做。只要修改答案,我就接受它。我看到的明显缩短是设置一个布尔值来表示“order_by_value”字符串中存在“!”,从中修剪“!”并删除所有存在“case”!…”的情况,然后,如果布尔值为true,则反转“returnList”列表。
returnList.OrderByProperty(apiResponseTime.order_by_value)
returnList.OrderByProperty(apiResponseTime.order_by_value)