可以做得更干净,更短吗?

时间:2019-01-16 12:21:49

标签: c# .net

我有一个我认为不好的代码,因为它在每个case语句中都有类似的语句:

if (command.OrderProperty.ToLower().Equals("asc"))
{
   accessForms.Forms = accessForms.Forms.OrderBy(o => o.NumberForSort).ToList();
}
else
{
   accessForms.Forms = accessForms.Forms.OrderByDescending(o => o.NumberForSort).ToList();
}

唯一不同的是查询所依据的属性

switch (command.SortProperty.ToLower())
{
   case "number":
      if (command.OrderProperty.ToLower().Equals("asc"))
      {
         accessForms.Forms = accessForms.Forms.OrderBy(o => o.NumberForSort).ToList();
      }
      else
      {
         accessForms.Forms = accessForms.Forms.OrderByDescending(o => o.NumberForSort).ToList();
      }
      break;
   case "type":
      if (command.OrderProperty.ToLower().Equals("asc"))
      {
         accessForms.Forms = accessForms.Forms.OrderBy(o => o.Type).ToList();
      }
      else
      {
         accessForms.Forms = accessForms.Forms.OrderByDescending(o => o.Type).ToList();
      }
      break;
   case "employeename":
      if (command.OrderProperty.ToLower().Equals("asc"))
      {
         accessForms.Forms = accessForms.Forms.OrderBy(o => o.EmployeeName).ToList();
      }
      else
      {
         accessForms.Forms = accessForms.Forms.OrderByDescending(o => o.EmployeeName).ToList();
      }
      break;
   case "requestingemployeename":
         (...)

}

accessForms.FormsList<>。我想使这段代码更加简洁明了,但我不知道如何实现。

3 个答案:

答案 0 :(得分:1)

下面的方法应该起作用,基本上是构建一个从窗体中提取排序字段并在Linq操作中使用它的功能。

请注意,如果排序字段例如都是字符串,则可以使用类型更强的func。

Func<Form, object> sortFieldGetter = null;

switch (command.SortProperty.ToLower())
{
   case "number":
       sortFieldGetter = form => (object)form.NumberForSort;
       break;

   case "type":
       sortFieldGetter = form => (object)form.Type;
       break;

   case "employeename":
       sortFieldGetter = form => (object)form.EmployeeName;
       break;

   ...
}

if (command.OrderProperty.ToLower().Equals("asc"))
{
    accessForms.Forms = accessForms.Forms.OrderBy(sortFieldGetter).ToList();
}
else
{
    accessForms.Forms = accessForms.Forms.OrderByDescending(sortFieldGetter).ToList();
}

答案 1 :(得分:0)

使用写为here的@rob代码,您的最终代码如下所示:

 accessForms.Forms = command.OrderProperty.ToLower().Equals("asc")) ?
     accessForms.Forms.OrderBy(command.SortProperty.ToLower()).ToList() :
     accessForms.Forms.OrderByDescending(command.SortProperty.ToLower()).ToList();

请注意,您需要实现原始代码中没有的自己的OrderByDescending。

答案 2 :(得分:0)

我同意这个问题也许更适合代码审查。

在任何情况下,您都可以按照以下方式进行操作:

第1步:创建扩展方法

public static IQueryable<T> SortByField<T, TKey>(this IQueryable<T> source, Expression<Func<T, TKey>> selector, bool ascending = true)
{
    return ascending ? source.OrderBy(selector) : source.OrderByDescending(selector);
}

public static IQueryable<T> SortByField<T, TKey>(this IQueryable<T> source, Expression<Func<T, TKey>> selector, string order = "asc")
{
    return SortByField(source, selector, order.Equals("asc", StringComparison.OrdinalIgnoreCase)); //condition here can be improved, just as a sample of how to do it
}

第2步:享受:

 var samples = new List<Sample>().AsQueryable(); //you can of course use LINQ to SQL or EF, but I am lazy
 samples.SortByField(t => t.NumberForSort, false); //or "asc"

我认为改进您的“命令” /查询对象将有助于清理代码。

Rant:“ Case”对我来说是不得已的手段,它几乎总是意味着我必须在至少两个地方更改代码,这些地方可能并不总是位于同一程序集中,并且几乎使用了“ case” ”可以替换为“词典”或“策略”模式。