在C#中排序数据表中的行按长度数据表、长度

2023-09-04 02:47:15 作者:想象之中与众不同°

如何做到以下情形: 我有一些数据表,其中包含例如一些行:

How to do following scenario: I have some DataTable which contains for example some rows:

1.rowa   2.rowab   3.row   4.rowaba   ...   ñ。 rowabba

1.rowa 2.rowab 3.row 4.rowaba ... n. rowabba

如何通过lenght排序行,叫不上名字。我婉通过字段长度排序表。

How to sort rows by lenght, not by name. I wan to to sort Table by length of fields.

推荐答案

您可以添加一个额外的列到你的数据表,提供包含通话的前pression到 len个()的功能,从而导致该列的值进行自动计算:

You could add an extra column to your DataTable, supplying an expression containing a call to the len() function, causing the column's values to be automatically computed:

table.Columns.Add("LengthOfName", typeof(int), "len(Name)");

那么你可以简单排序在你之前,结合新列的数据表来您打算使用任何类型的UI控件的网格:

Then you can simply sort on your new column before binding the DataTable to a grid to whatever kind of UI control you plan to use:

table.DefaultView.Sort = "LengthOfName";