应用场景:流程操作记录轮次判定;
实现思路:对数据进行分组,然后循环,通过FindIndex()方法确定每一项在所在组的index,即该条数据的轮次信息。
简单实现:
List<Cs> list = new List<Cs>() {
new Cs(){ Name="a",Value=1},
new Cs(){ Name="b",Value=2},
new Cs(){ Name="c",Value=3},
new Cs(){ Name="a",Value=4},
new Cs(){ Name="b",Value=5},
new Cs(){ Name="c",Value=6},
new Cs(){ Name="a",Value=7},
new Cs(){ Name="b",Value=8},
new Cs(){ Name="c",Value=9},
new Cs(){ Name="a",Value=10},
new Cs(){ Name="b",Value=11},
new Cs(){ Name="c",Value=12},
new Cs(){ Name="a",Value=13},
new Cs(){ Name="b",Value=14},
new Cs(){ Name="c",Value=15},
new Cs(){ Name="a",Value=16},
new Cs(){ Name="b",Value=17},
new Cs(){ Name="c",Value=18},
new Cs(){ Name="d",Value=19},
};
//如何快速的将以上数据分组并标注属于哪一组
List<Test> result = new List<Test>();
foreach (var item in list.GroupBy(a => a.Name))
{
var lList = item.ToList().OrderBy(a => a.Value).ToList();
for (int i = 0; i < lList.Count(); i++)
{
var iii = lList[i];
var index = lList .FindIndex(e => e.Value == iii.Value);
var oldIndex = list.FindIndex(e => e.Value == iii.Value && e.Name == iii.Name);
result.Add(new Test()
{
Round = index + 1,
Name = item.Key,
Value = iii.Value,
Data = oldIndex > 0 ? list[oldIndex - 1].Value + "" : "0"
});
}
}
result = result.OrderBy(a => a.Value).ToList();
具体应用
List<HiddentRoundModel> resultRounds = new List<HiddentRoundModel>();
var htGovernprogressItemList = htGovernprogressList.Where(a => a.Ht_CheckResultForContentID == p.HiddentId).OrderBy(a => a.HandleDateTime).ToList();
foreach (var htGovernprogressGroup in htGovernprogressItemList.GroupBy(a => a.CheckContentStatu))
{
var htGroupList = htGovernprogressGroup.ToList().OrderBy(a => a.HandleDateTime).ToList();
foreach (var htGroess in htGroupList)
{
//同一组内找轮次
var round = htGroupList.FindIndex(a => a.HandleDateTime == htGroess.HandleDateTime && a.CheckContentStatu == htGroess.CheckContentStatu && a.NextNode == htGroess.NextNode);
//总队列中找当前索引,方便获取上个节点数据
var index = htGovernprogressItemList.FindIndex(a => a.HandleDateTime == htGroess.HandleDateTime && a.CheckContentStatu == htGroess.CheckContentStatu && a.NextNode == htGroess.NextNode);
resultRounds.Add(new HiddentRoundModel()
{
Round = round + 1,
HandleNodeNameStatus = ( (HiddenTroubleStatuEnum)htGroess.CheckContentStatu ).ToString(),
HandleDateTime = htGroess.HandleDateTime,
NextNodeNameStatus = ( (HiddenTroubleStatuEnum)htGroess.NextNode ).ToString(),
HandleUnitId = htGroess.HandleUnitID.ToString(),
HandleUnitName = htGroess.HandleUnitName,
HandleUserId = htGroess.HandleUserID.ToString(),
HandleUserName = htGroess.HandleUserName,
PreHandleDateTime = index > 0 ? htGovernprogressItemList[index - 1].HandleDateTime : p.CheckDateTime,
IsReturn = htGroess.IsReturn,
HandleContent = htGroess.Content,
IsNew = htGroess.IsNew
});
}
}
resultRounds = resultRounds.OrderBy(a => a.HandleDateTime).ToList();