DataTable sort test question
I was asked this question recently in a test, which I didn't pass. For the life of me, I couldn't spot what the problem was. It's probably something really obvious as well, but even a colleague of mine couldn't spot it either. The best I could come up with was issues with bottlenecks and naming inconsistencies of parameters! I blame it on the fact I've not done any vb.net for years, I've been doing C# mostly these days :)
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
The开发者_运维问答 question: This function, although it will successfully sort a datatable, has a major problem with it. What is the problem? Re-write the function in either C# or VB.Net using LINQ.
The DataTable is cloned and then merged with it own clone? Very strange..
in c# i use
use an typed dataset create a datatable in precising type of data
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;
精彩评论