将自定义数据表排序转换为LINQ或lambda表达式

时间:2010-08-04 22:11:56

标签: c# linq lambda

我有一个很好的自定义排序例程,但我很好奇我将如何转换它以使用LINQ或lambda更少的代码。 SortByGrade对表示等级(K-12)的字符串值进行排序,使其返回:K,1,2,3,4等,而不是1,10,2等等

    /// <summary>
    /// Sorts a grade ( K - 12 ) numerically
    /// </summary>
    /// <remarks>A sort column is added and then sorted</remarks>
    /// <param name="table">table to sort</param>
    /// <param name="columnToSort">column to sort</param>
    /// <param name="sortDirection">enum of either asc or desc</param>
    /// <returns>Sorted data table</returns>
    public static DataTable SortByGrade(DataTable table, string columnToSort, SortDirection sortDirection)
    {
        string tmpSortColumn = "TempSortColumn";
        table.Columns.Add(tmpSortColumn, typeof(int));

        foreach (DataRow row in table.Rows)
        {
            row[tmpSortColumn] = ConvertGrade(row[columnToSort].ToString());
        }

        // sort on our need sort column
        table.DefaultView.Sort = String.Format("{0} {1}", tmpSortColumn, sortDirection.ToString());

        // return the sorted table
        return table.DefaultView.ToTable();
    }

并排序助手:

private static int ConvertGrade(string grade)
    {   
        int convertedGrade;
        switch(grade.ToUpper().Trim())
        {
            case "K":
                convertedGrade = 0;
                break;
            case "1":
                convertedGrade = 1;
                break;
            case "2":
                convertedGrade = 2;
                break;
            case "3":
                convertedGrade = 3;
                break;
            case "4":
                convertedGrade = 4;
                break;
            case "5":
                convertedGrade = 5;
                break;
            case "6":
                convertedGrade = 6;
                break;
            case "7":
                convertedGrade = 7;
                break;
            case "8":
                convertedGrade = 8;
                break;
            case "9":
                convertedGrade = 9;
                break;
            case "10":
                convertedGrade = 10;
                break;
            case "11":
                convertedGrade = 11;
                break;
            case "12":
                convertedGrade = 12;
                break;
            // TODO: Remove these cases when the data is cleaned up
            case "00":
            case "17":
                convertedGrade = 100;
                break;
            default:
                convertedGrade = 0;
                break;
        }
return convertedGrade;
    }

非常感谢。

1 个答案:

答案 0 :(得分:2)

您可以将DataTableExtensions用于AsEnumerableCopyToDataTable方法。

public static DataTable SortByGrade(DataTable table,
  string columnToSort, SortDirection sortDirection) 
{

  IEnumerable<DataRow> query = 
    from row in table.AsEnumerable()
    orderby ConvertGrade(row[columnToSort].ToString())
    select row;

  if (sortDirection == "DESC")
  {
    query = query.Reverse();
  }

  DataTable result = query.CopyToDataTable();
  return result;
}

并排序助手:

private static int ConvertGrade(string grade)  
{
  string g = grade.ToUpper().Trim();
  int convertedGrade =
    g == "K" ? 0 :
    g == "1" ? 1 :
    g == "2" ? 2 :
    g == "3" ? 3 :
    g == "4" ? 4 :
    g == "5" ? 5 :
    g == "6" ? 6 :
    g == "7" ? 7 :
    g == "8" ? 8 :
    g == "9" ? 9 :
    g == "10" ? 10 :
    g == "11" ? 11 :
    g == "12" ? 12 :
// TODO: Remove these cases when the data is cleaned up
    g == "00" ? 100 :  
    g == "17" ? 100 :
    0;  

  return convertedGrade;  
}