问:List<string> list = new List<string>(); for (int i = 1; i < 95; i++) { list.Add(i.ToString()); }如何将list拆分成10个子集合?
答案:
List<List<string>> listGroup = new List<List<string>>();
int j = 10;
for (int i = 0; i < list1.Count; i += 10)
{
List<string> cList = new List<string>();
cList = list1.Take(j).Skip(i).ToList();
j +=10;
listGroup.Add(cList);
}
个人使用中的实例如下:
#region 新方法
List<List<hourhistoryrecorditem>> listGroup = new List<List<hourhistoryrecorditem>>();
int j = 500000;
for (int i = 0; i < lstVals.Count; i += 500000) //以50万为一组分组
{
List<hourhistoryrecorditem> cList = new List<hourhistoryrecorditem>();
cList = lstVals.Take(j).Skip(i).ToList();
j += 500000;
listGroup.Add(cList);
}
for (int i = 0; i < listGroup.Count; i++)
{
List<hourhistoryrecorditem> list = new List<hourhistoryrecorditem>();
list = listGroup[i];
foreach (hourhistoryrecorditem aItem in list)
{
int iOffset = 0;
DataRow r = dt.NewRow();
r[iOffset++] = DBNull.Value;
r[iOffset++] = aItem.CurValue;
r[iOffset++] = aItem.MaxValue_;
r[iOffset++] = aItem.MinValue;
r[iOffset++] = aItem.AvgValue;
r[iOffset++] = aItem.ValueFlag;
r[iOffset++] = aItem.RecordType;
r[iOffset++] = aItem.NodeID;
r[iOffset++] = aItem.LogDT;
r[iOffset++] = aItem.DataCount;
r[iOffset++] = aItem.Increment;
r[iOffset++] = aItem.VTopVal;
r[iOffset++] = aItem.TopVal;
r[iOffset++] = aItem.NormalVal;
r[iOffset++] = aItem.LowVal;
r[iOffset++] = aItem.VLowVal;
r[iOffset++] = aItem.ChargeVal;
r[iOffset++] = aItem.IncVTopVal;
r[iOffset++] = aItem.IncTopVal;
r[iOffset++] = aItem.IncNormalVal;
r[iOffset++] = aItem.IncLowVal;
r[iOffset++] = aItem.IncVLowVal;
r[iOffset++] = aItem.IncChargeVal;
r[iOffset++] = aItem.OrgID;
dt.Rows.Add(r);
}
TablesBlukInsertOp.BulkToDB(dt, "hourhistoryrecorditem");
dt.Rows.Clear(); //执行完插入后,记得清空DataTable
}
#endregion