随机播放特定的数据表列值



我想打乱数据表列值。我的表如下所示:

+------+------+------+------+------+
| col1 | col2 | col3 | col4 | col5 |
+------+------+------+------+------+
|   11 |   12 |   13 |   14 |   15 |
|   21 |   22 |   23 |   24 |   25 |
|   31 |   23 |   33 |   34 |   25 |
+------+------+------+------+------+

现在,我想发生的是随机洗牌col2col3col4的单元格值。例:

+------+------+------+------+------+
| col1 | col2 | col3 | col4 | col5 |
+------+------+------+------+------+
|   11 |   14 |   12 |   13 |   15 |
|   21 |   23 |   24 |   22 |   25 |
|   31 |   32 |   34 |   33 |   35 |
+------+------+------+------+------+

这是我到目前为止的代码。

private void shuffleDT()
{
DataTable sdt = new DataTable();
sdt.Columns.Add("col1", typeof(string));
sdt.Columns.Add("col2", typeof(string));
sdt.Columns.Add("col3", typeof(string));
sdt.Columns.Add("col4", typeof(string));
sdt.Columns.Add("col5", typeof(string));
sdt.Rows.Add(new object[] { "11", "12", "13", "14", "15" });
sdt.Rows.Add(new object[] { "21", "22", "23", "24", "25" });
sdt.Rows.Add(new object[] { "31", "32", "33", "34", "35"});
}

这是一个Random的解决方案

private DataTable sdt = new DataTable();
private void InitDt()
{
sdt = new DataTable();
sdt.Columns.Add("col1", typeof(string));
sdt.Columns.Add("col2", typeof(string));
sdt.Columns.Add("col3", typeof(string));
sdt.Columns.Add("col4", typeof(string));
sdt.Columns.Add("col5", typeof(string));
sdt.Rows.Add(new object[] { "11", "12", "13", "14", "15" });
sdt.Rows.Add(new object[] { "21", "22", "23", "24", "25" });
sdt.Rows.Add(new object[] { "31", "23", "33", "34", "25" });
}
private void ShuffleDT()
{
Random loRan = new Random(DateTime.Now.Millisecond);
int lnRan;
string[] loRowValues = new string[3];
List<int> loList;
foreach (DataRow loRow in sdt.Rows)
{
loList = new List<int>();
while (loList.Count < 3)
{
if (loList.Count == 2)
loList.Add(new List<int>() { 0, 1, 2 }.Except(loList).First());
else
{
lnRan = loRan.Next(0, 3);
if (!loList.Contains(lnRan))
loList.Add(lnRan);
}
}
loRowValues[0] = loRow.Field<string>(1);
loRowValues[1] = loRow.Field<string>(2);
loRowValues[2] = loRow.Field<string>(3);
loRow.SetField<string>(1, loRowValues[loList[0]]);
loRow.SetField<string>(2, loRowValues[loList[1]]);
loRow.SetField<string>(3, loRowValues[loList[2]]);
}
}

最新更新