无法使用System.Linq.Dynamic进行排序

时间:2013-12-31 13:55:16

标签: c# linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Dynamic;

namespace LinqTest
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Sample> sampleData = new List<Sample>() 
            { 
                new Sample{ ID=1, Name="V", Designation="Executive", JoiningDate=DateTime.Now.Date, Salary=45000M },
                new Sample{ ID=2, Name="F", Designation="Executive", JoiningDate=DateTime.Now.Date, Salary=400M },
                new Sample{ ID=3, Name="A", Designation="Boss", JoiningDate=DateTime.Now.Date, Salary=5000M },
                new Sample{ ID=1, Name="D", Designation="Executive", JoiningDate=DateTime.Now.Date, Salary=2500M },
                new Sample{ ID=1, Name="V", Designation="Executive", JoiningDate=DateTime.Now.Date, Salary=45000M }
            };

            IQueryable<Sample> sas = sampleData.AsQueryable<Sample>();
            sas.OrderBy("Name");
        }
    }
}

正如您在上面所看到的,我正在尝试使用System.Linq.Dynamic库的扩展方法对其中一个列进行简单排序。但它没有被分类。当我将鼠标悬停在sas上时,我会看到原始订单中的商品。有人可以告诉我为什么吗?

1 个答案:

答案 0 :(得分:7)

原始sas变量仍然无序。您需要保存OrderBy结果

var myOrderedSas = sas.OrderBy("Name");