DataView.Sort和DataTable.OrderBy()之间的区别.AsDataView()?

时间:2010-10-07 16:10:49

标签: c# linq dataview

我有DataGridView绑定到DataTable,其中包含我需要排序的自定义类型列。此自定义类型(MyCustomType)实现IComparable<MyCustomType>,这就是我希望排序工作的方式。

我尝试了2种方法来创建DataView,我将其用作网格的数据源:

MyDataTable dt = new MyDataTable();
DataView dv = new DataView(dt);
dv.Sort = "MyCustomField";

这不能正常工作 - 它“几乎”有效,但有时我的行不能正确排序。

第二种方法:

MyDataTable dt = new MyDataTable();
DataView dv = dt.OrderBy(row => row.MyCustomField).AsDataView();

这似乎正在做我想要的。我的问题是,这两种方法有什么区别?使用我的DataView.Sort实施,IComparable<T> 是否可能?启用LINQ的DataView ?为什么会这样?

此外,是否有人知道非LINQ启用和启用LINQ的DataView的相对性能?似乎没有巨大的性能损失,没有任何理由使用非LINQ启用的版本。

3 个答案:

答案 0 :(得分:1)

我正在发布比较,其他人想知道Linq-DataView与非Linq-DataView的表现如何,特别是因为结果让我感到惊讶。对于此测试,至少旧的DataView比启用Linq的DataView快一个数量级。

v1: Linq-DataView, on-the-fly sort string -> OrderBy/ThenBy via Field<dynamic>()
v2: Linq-DataView, on-the-fly via mapped Field<type>()
v3: Linq-DataView, hard-coded OrderBy/ThenBy
v4: non-Linq DataView w/sort string

Linq-DataView与非Linq-DataView的无类型dtbl(秒)

03.411  v1 = dtbl.AsEnumerable().OrderBy("T30y, Dat desc").AsDataView();
02.561  v2 = dtbl.AsEnumerable().OrderBy(dtbl, "T30y, Dat desc").AsDataView();
01.573  v3 = dtbl.AsEnumerable().OrderBy(y=>y.Field<decimal>("T30y"))
                                .ThenByDescending(y=>y.Field<DateTime>("Dat")).AsDataView();
00.214  v4 = new DataView(dtbl, "", "T30y, Dat desc", DataViewRowState.CurrentRows);

02.403  v1: 100,000 iterations of Find()
01.708  v2: 100,000 iterations of Find()
01.173  v3: 100,000 iterations of Find()
00.261  v4: 100,000 iterations of Find()

Order for v2(带有v1的内嵌注释)

static public EnumerableRowCollection<DataRow>
    OrderBy( this EnumerableRowCollection<DataRow> ys, DataTable dtbl, string sort )
{
    OrderedEnumerableRowCollection<DataRow> oys = null;
    foreach ( string s in (sort ?? "").Split(new []{", "}, StringSplitOptions.None) )
    {
        int n = s.IndexOf(" desc");
        string x = n!=-1 ? s.Substring(0, n) : s;
        Type typ = dtbl.Columns[x].DataType;
        Func<DataRow,dynamic> vfn = y=>yget[typ](y,x); // v1: vfn = y.Field<dynamic>(x)

        if ( oys==null )
             oys = s.Contains(" desc") ? ys.OrderByDescending(vfn) : ys.OrderBy(vfn);
        else oys = s.Contains(" desc") ? oys.ThenByDescending(vfn) : oys.ThenBy(vfn);
    }
    return oys ?? ys;
}

static Dictionary<Type,Func<DataRow,string,dynamic>>
    yget = new Dictionary<Type,Func<DataRow,string,dynamic>>
{
    {typeof(bool),     (y,x)=>y.Field<bool>(x)},
    {typeof(short),    (y,x)=>y.Field<short>(x)},
    {typeof(int),      (y,x)=>y.Field<int>(x)},
    {typeof(string),   (y,x)=>y.Field<string>(x)},
    {typeof(decimal),  (y,x)=>y.Field<decimal>(x)},
    {typeof(float),    (y,x)=>y.Field<float>(x)},
    {typeof(double),   (y,x)=>y.Field<double>(x)},
    {typeof(DateTime), (y,x)=>y.Field<DateTime>(x)},
    {typeof(TimeSpan), (y,x)=>y.Field<TimeSpan>(x)},
};

如果有人见过这个并且可以建议一种映射数据列的方法 - &gt;现场lambda不依赖于Func&lt;&gt;返回动态类型,任何建议都是最受欢迎的。

答案 1 :(得分:0)

当您在DataView上调用.Sort时,它会对该对象的特定实例进行排序。您的第二种方法实际上创建了一个全新的对象并分配给dv变量。这可能是一个关键的区别。

编辑:不是你的第二种方法,因为你没有分配,然后重新分配。但是如果您有一个现有的DataView,然后使用OrderBy方法重新分配它,那么您将拥有我建议的场景。

对不起,我不像以前那么清楚。

答案 2 :(得分:-1)

DataView.Sort是一个内置在DataView类中的方法,而.OrderBy是一个扩展。