【问题标题】:DataTable sort test question数据表排序测试题
【发布时间】:2010-07-22 15:47:22
【问题描述】:

我最近在一次测试中被问到这个问题,但我没有通过。对于我的生活,我无法发现问题所在。这可能也很明显,但即使是我的一位同事也无法发现它。我能想到的最好的办法是瓶颈和参数命名不一致的问题!我把它归咎于我多年来没有做过任何 vb.net 的事实,这些天我大部分时间都在做 C# :)

Private Function sorttable(ByVal dt As DataTable, ByVal sorttype$, ByVal sort_direction$) As DataTable
    Dim dv As DataView
    Dim dt2 As DataTable
    dt2 = dt.Clone
    dt2.Merge(dt)
    dv = dt2.DefaultView
    dv.Sort = sorttype & " " & sort_direction
    Return dv.ToTable()
End Function

问题:这个函数虽然可以成功地对数据表进行排序,但它有一个大问题。问题是什么?使用 LINQ 在 C# 或 VB.Net 中重新编写函数。

【问题讨论】:

    标签: vb.net sorting datatable


    【解决方案1】:

    DataTable 被克隆然后与它自己的克隆合并?很奇怪..

    【讨论】:

      【解决方案2】:

      在 c# 中我使用

      使用类型化数据集 创建一个精确数据类型的数据表

      for example i have create a dsAppointment
      
           DsAppointment dsAppointmentTmp = new DsAppointment();
           DsAppointment dsAppointment = new DsAppointment();
          //add all appointment
           dsAppointmentTmp.Appointment.AddAppointmentRow(name,start,end,body)
          //use select(filter,sort(name of columns)
          DataRow[] rows1 = dsAppointmentTmp.Tables[0].Select(string.Empty, dsAppointmentTmp.Tables[0].Columns[1].ToString());
      
                      foreach (DataRow thisRow in rows1)
                      {
                              dsAppointment.Tables[0].Rows.Add(thisRow.ItemArray);
      
                      }
      //return dsAppointment sorted
                      return dsAppointment;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-21
        • 2015-01-26
        • 2017-11-11
        • 1970-01-01
        • 2017-03-04
        • 1970-01-01
        相关资源
        最近更新 更多