datatable select order

  • Thread starter Thread starter MarcG
  • Start date Start date
M

MarcG

I have a datatable that I create from scratch, first adding a bunch of
columns all of datatype String. The columns are named. There are no keys or
constraints defined for the table or the columns.

I programmatically load the table, one row at a time from a tab delimited
file (I can't use the Text Trieber for various reasons that are not
relevant). The records have a recognizable pattern and I verify after the
load that the rows are in the expected order.

I do a table.Select("not (TrialNo = '.')") and the rows come back ordered by
the TrialNo column.

Doing a table.Select("(not (BlockNo = '.')) and (TrialNo = '.')" ) produces
an ordering by BlockNo (the data is inconclusive about TrialNo ordering in
this case).

According to the docs, the data is supposed to come back ordered by primary
key if there is one (there is not in my table), or row insertion order.

Apparently in the absence of a primary key it is using the selected columns
as though I had coded:
datatable.Select("not (TrialNo = '.')", "TrialNo ASC")

This is a problem in my app. Since I am building the tables based on the
data in the files, I can't in general derive a sort expression that will put
everything back into insertion order, which is exactly what I want.

VS2005, Framework 2


Bug?? Work around?

Thx
Marc
 
Hello Marc,

Thanks for your posting. It seems you want to get the rows from
Table.Select() method ordered by insertion order, correct? If I
misunderstood anything here, please don't hesitate to correct me.

I have reproduced the issue on my side. According to MSDN Document,
Select() method should return an array of all DataRow objects in order of
addition if lacking primary key. But it doesn't... This seems like a
product issue. I'm willing to log this issue in our DB for product team to
investigate. This will benefit our product quality improvement.
As workaround, I'd like to suggest you can use Dataview to filter the data.
This tool is much better than DataTable.Select() method. You can also set
the RowStateFilter in the view. It could also output datatable directly.

Please try the followin method.The result will be sored by addtion.
DataView dv = new DataView(yourTable);
dv.RowFilter = "(not (BlockNo = '.')) and (TrialNo = '.')";
DataTable dt2=dv.ToTable();

Hope this helps. Please try the above method and let us know if you have
any more concern. We are glad to assist you.
Have a great day,
Best regards,
Wen Yuan

Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
MarcG:
As another workaround - perhaps add an identity column yourself (i.e. using
the original >row number as a field), and simply sort by that? crude, but
it should work.

Marc G [a different one; this could get confusing]
 
Marc,

The Identity column was going to be my fallback solution. I was trying to
avoid it because I'm loading multiple files into the table and because of
versioning issues, some might have more columns than others and I didn't want
to have to differentiate the fake key column from real data columns.

I followed Wen's suggestion about using the DataView class and it works like
a dream. I've had the it on my list of things to learn about but hadn't got
to it yet.


Wen, as usual thanks for the help. As usual, you were right on target. I
wonder if they fixed it in Framework 3. ??

Marc
 
Hello Marc,
Thanks for your reply.

I have tried it on .net frame work 3.5. But, nothing changed.
DataTable.Select() still sorts the resolute rows. DataView is a good way to
work around in your scenario. Acutully, the implementation of
DataTable.Select() method will create an internal DataView to filter the
rows. Thereby, there won't be performance issue after you changes solution.

Please feel free to let us know, if you have any more concern or there is
anything unclear. It's my pleasure to assist you.:)

Have a great day,
Best regards,
We Yuan

Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
Suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top