降低(我的)Linq列表排序的详细程度

时间:2012-03-08 04:50:31

标签: c# linq sorting

我有一个类型化的DataTable,它可以用来排序:

DataTable.DefaultView.Sort("sortexpression");

因为sort表达式是一个字符串,所以我可以将排序字段和方向附加在几行中,无论我有多少排序选项。现在Linq Im显然做了一些非常错误的事情,因为我做了类似的事情:

             this.GetSortExpressions();
        if ((ViewState["SortDirection"] as string) == "ASC")
        {
            switch (ViewState["SortField"] as string)
            {
                case "LKey":
                    this.SortedDetails = this.Details.OrderBy(d => d.LKey);
                    break;
                case "MName":
                    this.SortedDetails = this.Details.OrderBy(d => d.MaterialName);
                    break;
                case "FMSQOH":
                    this.SortedDetails = this.Details.OrderBy(d => d.FMSQOH);
                    break;
                case "CCQOH":
                    this.SortedDetails = this.Details.OrderBy(d => d.CCQOH);
                    break;
                case "FMSQOHVary":
                    this.SortedDetails = this.Details.OrderBy(d => d.FMSQOHVary);
                    break;
                default:
                    this.SortedDetails = this.Details.OrderBy(d => d.LKey);
                    break;
            }
        }
        else
        {
            switch (ViewState["SortField"] as string)
            {
                case "LKey":
                    this.SortedDetails = this.Details.OrderByDescending(d => d.LKey);
                    break;
                case "MName":
                    this.SortedDetails = this.Details.OrderByDescending(d => d.MaterialName);
                    break;
                case "FMSQOH":
                    this.SortedDetails = this.Details.OrderByDescending(d => d.FMSQOH);
                    break;
                case "CCQOH":
                    this.SortedDetails = this.Details.OrderByDescending(d => d.CCQOH);
                    break;
                case "FMSQOHVary":
                    this.SortedDetails = this.Details.OrderByDescending(d => d.FMSQOHVary);
                    break;
                default:
                    this.SortedDetails = this.Details.OrderByDescending(d => d.LKey);
                    break;
            }
        }

这太可怕了。我担心我为每个新的排序字段添加2 * n个case语句。请问正确的方法是什么?

2 个答案:

答案 0 :(得分:1)

我已经完成了这个...我最后发现的是一个使用反射来对IEnumerable对象进行排序的通用解决方案。

http://zhousanfeng.wordpress.com/2009/12/01/a-generic-comparersorter-class%E8%BD%AC/

这将是你如何使用它

Sorter<TestClass> sort = new Sorter<TestClass>(this.Deatils, "LKey ASC");
List<TestClass> sorted = sort.GetSortedList();

问候。

答案 1 :(得分:0)

在执行排序(未测试)之前,将Func分配给orderby方法:

public IOrderedEnumerable<TSource> OrderFunc<TSource, TKey>(
    this IEnumerable<TSource> source,
    Func<TSource, TKey> keySelector
);
OrderFunc order;
if ((ViewState["SortDirection"] as string) == "ASC")
    order = Enumerable.OrderBy;
else
    order = Enumerable.OrderByDescending;

switch (ViewState["SortField"] as string)
{
    case "LKey":
        this.SortedDetails = order(this.Details, d => d.LKey);
    break;
    case "MName":
        this.SortedDetails = order(this.Details, d => d.MaterialName);
    break;
    case "FMSQOH":
        this.SortedDetails = order(this.Details, d => d.FMSQOH);
    break;
    case "CCQOH":
        this.SortedDetails = order(this.Details, d => d.CCQOH);
    break;
    case "FMSQOHVary":
        this.SortedDetails = order(this.Details, d => d.FMSQOHVary);
    break;
    default:
        this.SortedDetails = order(this.Details, d => dLKey);
    break;
}