如何将对象列表拆分为按元素(日期时间和字符串)分组的子列表 C#



我试图根据主列表制作子列表,根据日期时间升序排列9条记录,然后是pcName(string(。

我的排序子列表需要如下所示

对象列表 1 :

2615,2019-11-22 16:03:22。150测试1

2615,2019-11-22 16:03:22。200测试1

2615,2019-11-22 16:03:22。250测试1

2615,2019-11-22 16:03:22。300测试1

对象列表 2 :

2615,2019-11-22 16:03:22。350测试2

2615,2019-11-22 16:03:22。400测试2

对象列表 3 :

2615,2019-11-22 16:03:22。450测试1

2615,2019-11-22 16:03:22。500测试1

对象列表 4 :

2615,2019-11-22 16:03:22。550测试3

此对象列表需要按照顺序取 对象列表[0

] ,对象列表[1] ,对象列表[2] 和对象列表[3]我在下面提到了我的示例代码

但这只为我提供了 3 个基于 Test1、Test2 和 Test3 和 9个子列表的子列表集 (Item1( 过滤器,但我想要上面的 4 个子列表。 我想在哪里更改代码?请帮助我

private void button_Click(object sender, EventArgs e)
{
List<Identity> listOfIdentity = new List<Identity>()
{
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.550"),PcName="Test3"},                              
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.300"), PcName="Test1"},                            
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.350"), PcName="Test2"},
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.400"), PcName="Test2"},
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.200"), PcName="Test1"},
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.500"), PcName="Test1"},
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.250"), PcName="Test1"},
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.450"), PcName="Test1"},
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.150"), PcName="Test1"}              
};
List<Identity> sortedIdentity = listOfIdentity.OrderBy(x => x.QCTime).ThenBy(x => x.PcName).ToList(); // here i order by time and then pc name
var items1 = listOfIdentity.OrderBy(x => x.QCTime).ThenBy(x => x.PcName).GroupBy(x => x.PcName).Select(grp => grp.ToList()).ToList(); //sub list create based with **Test1, Test2 & Test3** (3 sub lists)
var items2 = listOfIdentity.OrderBy(x => x.QCTime).ThenBy(x => x.PcName).GroupBy(x => new { x.QCTime, x.PcName }).Select(grp => grp.ToList()).ToList(); //sub list create based with each time and pc name (9 sublists)
}
class Identity
{
public int id { get; set; }
public DateTime QCTime { get; set; }
public string PcName { get; set; }
}

这是我使用Linq.Aggregate的解决方案:

private void button_Click(object sender, EventArgs e)
{
List<Identity> listOfIdentity = new List<Identity>()
{
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.550"), PcName="Test3"},
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.300"), PcName="Test1"},
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.350"), PcName="Test2"},
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.400"), PcName="Test2"},
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.200"), PcName="Test1"},
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.500"), PcName="Test1"},
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.250"), PcName="Test1"},
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.450"), PcName="Test1"},
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.150"), PcName="Test1"}
};
List<Identity> sortedIdentity = listOfIdentity.OrderBy(x => x.QCTime).ThenBy(x => x.PcName).ToList(); // here i order by time and then pc name
var result = sortedIdentity.Skip(1).Aggregate(new List<List<Identity>> { new List<Identity> { sortedIdentity[0] } }, (lists, curr) =>
{
if (curr.PcName == lists.Last()[0].PcName)
lists.Last().Add(curr);
else
lists.Add(new List<Identity> { curr });
return lists;
});
}

它遍历每个元素并检查它的PcName是否与以前的元素相同。如果是,则它位于同一列表中,否则将使用此元素创建新列表。


编辑:Matt.G建议的更优雅的解决方案

List<Identity> sortedIdentity = listOfIdentity.OrderBy(x => x.QCTime).ThenBy(x => x.PcName).ToList();
var result = sortedIdentity.Aggregate(new List<List<Identity>>(), (lists, curr) => 
{ 
if (curr.PcName == lists.LastOrDefault()?.FirstOrDefault()?.PcName)
lists.Last().Add(curr);
else
lists.Add(new List<Identity> { curr });
return lists;
});

没有专门用于一次性对连续元素进行分组的内置 LINQ 方法。 不过,创建一个并不难。在这个答案中可以找到一个实现,具有下面的签名:

public static IEnumerable<IEnumerable<TSource>> SplitByPredicate<TSource>(
this IEnumerable<TSource> source,
Func<TSource, TSource, bool> splitPredicate);

你可以用它来解决你的问题,就像这样:

var sublists = listOfIdentity
.OrderBy(x => x.QCTime)
.ThenBy(x => x.PcName)
.SplitByPredicate((x, y) => x.PcName != y.PcName);

可以在这里找到不同的实现,其签名略有不同:

public static IEnumerable<List<TSource>> GroupConsecutiveByKey<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector);

。可以这样使用:

var sublists = listOfIdentity
.OrderBy(x => x.QCTime)
.ThenBy(x => x.PcName)
.GroupConsecutiveByKey(x => x.PcName);

由于您尝试按日期时间列进行分组,并且仅以毫秒为单位变化,因此不会按照您的期望进行分组。我修改了您的代码,如下所示:

class Identity
{
public int id { get; set; }
public DateTime QCTime { get; set; }
public string PcName { get; set; }
public string QCTimeForGroup
{
get
{
if (QCTime.Millisecond <= 300)
{
return "SL1";
}
else if (QCTime.Millisecond > 300 && QCTime.Millisecond <= 400)
{
return "SL2";
}
else if (QCTime.Millisecond > 400 && QCTime.Millisecond <= 500)
{
return "SL3";
}
return "SL4";
}
}
}

private void button_Click(object sender, EventArgs e)
{
List<Identity> listOfIdentity = new List<Identity>()
{
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.550"),PcName="Test3"},                              
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.300"), PcName="Test1"},                            
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.350"), PcName="Test2"},
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.400"), PcName="Test2"},
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.200"), PcName="Test1"},
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.500"), PcName="Test1"},
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.250"), PcName="Test1"},
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.450"), PcName="Test1"},
new Identity() {id= 2615,QCTime=DateTime.Parse("2019-11-22 16:03:22.150"), PcName="Test1"}              
};
List<Identity> sortedIdentity = listOfIdentity.OrderBy(x => x.QCTime).ThenBy(x => x.PcName).ToList(); // here i order by time and then pc name
var items1 = listOfIdentity.OrderBy(x => x.QCTime).ThenBy(x => x.PcName).GroupBy(x => x.PcName).Select(grp => grp.ToList()).ToList(); //sub list create based with **Test1, Test2 & Test3** (3 sub lists)
var items2 = listOfIdentity.OrderBy(x => x.QCTime).ThenBy(x => x.PcName).GroupBy(x => new { x.QCTime, x.PcName }).Select(grp => grp.ToList()).ToList(); //sub list create based with each time and pc name (9 sublists)
var items3 = listOfIdentity.OrderBy(x => x.QCTime).ThenBy(x => x.PcName)
.GroupBy(x => new { x.QCTimeForGroup, x.PcName }).Select(grp 
=> grp.ToList()).ToList(); 
}

最新更新