Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何对数据表进行排序';s主键数组_Vb.net - Fatal编程技术网

Vb.net 如何对数据表进行排序';s主键数组

Vb.net 如何对数据表进行排序';s主键数组,vb.net,Vb.net,我试图从数据表中对主键数组进行排序。我找不到任何与我所要做的相符的例子 我需要比较两个datacolumns数组。这需要排序(至少使用我能想出的唯一方法) 我将在这里发布我问题的答案: 我正在努力找到一个有效的解决办法。我可以将名称复制到单独的数组中,对它们进行排序和比较,但这种方法很难维护代码。如果这是唯一的方法,那么我会这样做,但是这些方法名称向我暗示有更好的方法 数据表已填充,PrimaryKey数组已正确填充。但是,我无法让这些方法发挥作用: DTS.PrimaryKey.ToL

我试图从数据表中对主键数组进行排序。我找不到任何与我所要做的相符的例子

我需要比较两个datacolumns数组。这需要排序(至少使用我能想出的唯一方法)

我将在这里发布我问题的答案:

我正在努力找到一个有效的解决办法。我可以将名称复制到单独的数组中,对它们进行排序和比较,但这种方法很难维护代码。如果这是唯一的方法,那么我会这样做,但是这些方法名称向我暗示有更好的方法

数据表已填充,PrimaryKey数组已正确填充。但是,我无法让这些方法发挥作用:

    DTS.PrimaryKey.ToList().Sort(Function(x, y) x.Caption.CompareTo(y.Caption))
    DTT.PrimaryKey.ToList().Sort(Function(x, y) x.Caption.CompareTo(y.Caption))
或这些:

    DTS.PrimaryKey.OrderBy(Function(c) c.Caption)
    DTT.PrimaryKey.OrderBy(Function(c) c.Caption)
在得到相同结果之前和之后:

?DTS.PrimaryKey(0).Caption
"Manufacturer"
?DTS.PrimaryKey(1).Caption
"Make"
?DTT.PrimaryKey(0).Caption
"Manufacturer"
?DTT.PrimaryKey(1).Caption
"Style"
DTS和DTT都定义为System.Data.DataTable。 我创建了两个简单的测试表:

CREATE TABLE [dbo].[AAA](
    [Manufacturer] [nchar](10) NOT NULL,
    [Make] [nchar](10) NOT NULL,
    [Color] [nchar](10) NULL,
 CONSTRAINT [PK_AAA] PRIMARY KEY CLUSTERED 
(
    [Manufacturer] ASC,
    [Make] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,     ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

我不在乎它们的顺序(升序或降序),只要是某种顺序:

?DTS.PrimaryKey(0).Caption
"Make"
?DTS.PrimaryKey(1).Caption
"Manufacturer"
?DTT.PrimaryKey(0).Caption
"Manufacturer"
?DTT.PrimaryKey(1).Caption
"Style"

编辑:

我使用以下代码进行排序:

    Dim s = DTS.PrimaryKey.OrderBy(Function(c) c.Caption)
    Dim t = DTT.PrimaryKey.OrderBy(Function(c) c.Caption)
但我不喜欢编写让跟随我的程序员猜不透的代码。我更愿意明确定义这些变量。当我检查得到的类型时:

"System.Linq.OrderedEnumerable`2[System.Data.DataColumn,System.String]"
这看起来有点奇怪。另外,我似乎无法明确定义这种类型的变量。这似乎与强类型变量背道而驰

    Dim s As IOrderedEnumerable(Of DataColumn) = dt.PrimaryKey.OrderBy(Function(c) c.Caption)

这是强类型的,没有显式声明,因为编译器知道数据类型。

Perfect!!非常感谢。
"System.Linq.OrderedEnumerable`2[System.Data.DataColumn,System.String]"
    Dim s As IOrderedEnumerable(Of DataColumn) = dt.PrimaryKey.OrderBy(Function(c) c.Caption)