0

我有一个 asp.net 数据表,我想将数据绑定到两个 asp.net 数据列表中,所以我想将两个数据表中的数据表切片,即使 .

4

2 回答 2

2

使用TakeLINQ 扩展方法指定要使用的项目数。

如果需要,Skip可以跳过。

var half = myList.Take(myList.Count / 2);
于 2012-11-15T11:21:27.050 回答
0

如果您按行切片,您可以简单地创建原始数据表的副本,找到合适的中间点,然后将行导入副本,同时从原始数据表中删除它们。

像下面这样的东西应该可以工作:

DataTable originalTable = new DataTable();
//Load the data into your original table or wherever you get your original table from

DataTable otherTable = originalTable.Copy(); //Copys the table structure only - no data

int rowCount = originalTable.Rows.Count;
int wayPoint = rowCount / 2; //NB integer division rounds down towards 0

for(int i = 0; i <= wayPoint; i++)
{
    otherTable.ImportRow(originalTable.Rows[i]); //Imports (copies) the row from the original table to the new one
    originalTable.Rows[i].Delete(); //Marks row for deletion
}

originalTable.AcceptChanges(); //Removes the rows we marked for deletion
于 2012-11-15T11:47:11.860 回答