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

如何将对象列表拆分为按元素(日期时间和字符串)分组的子列表C#,c#,linq,sublist,tobjectlist,C#,Linq,Sublist,Tobjectlist,我尝试根据datetime的升序和pcName(字符串),在包含9条记录的主列表的基础上创建子列表 我的排序子列表需要如下所示 对象列表1: 26152019-11-22 16:03:22.150,Test1 26152019-11-22 16:03:22.200,Test1 26152019-11-22 16:03:22.250,Test1 26152019-11-22 16:03:22.300,Test1 对象列表2: 26152019-11-22 16:03:22.350,Test2 26

我尝试根据datetime的升序和pcName(字符串),在包含9条记录的主列表的基础上创建子列表

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

对象列表1:

26152019-11-22 16:03:22.150Test1

26152019-11-22 16:03:22.200Test1

26152019-11-22 16:03:22.250Test1

26152019-11-22 16:03:22.300Test1

对象列表2:

26152019-11-22 16:03:22.350Test2

26152019-11-22 16:03:22.400Test2

对象列表3:

26152019-11-22 16:03:22.450Test1

26152019-11-22 16:03:22.500Test1

对象列表4:

26152019-11-22 16:03:22.550Test3

此对象列表需要按照ObjectList[0]、ObjectList[1]、ObjectList[2]和ObjectList[3]的顺序执行

我在下面提到了我的示例代码

但这只提供了3个子列表集(Item1)过滤器,基于Test1、Test2&Test3和9个子列表,但我需要上述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; }
        }
private void按钮\u单击(对象发送者,事件参数e)
{
List listOfIdentity=新列表()
{
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 sortedIdentity=listOfIdentity.OrderBy(x=>x.QCTime).ThenBy(x=>x.PcName).ToList();//这里我按时间排序,然后是电脑名
var items1=listOfIdentity.OrderBy(x=>x.QCTime)。然后by(x=>x.PcName)。GroupBy(x=>x.PcName)。选择(grp=>grp.ToList()).ToList();//使用**Test1、Test2和Test3**(3个子列表)创建子列表
var items2=listOfIdentity.OrderBy(x=>x.QCTime).然后by(x=>x.PcName).GroupBy(x=>new{x.QCTime,x.PcName}).选择(grp=>grp.ToList()).ToList();//根据每个时间和pc名称创建子列表(9个子列表)
}
类标识
{
公共int id{get;set;}
公共日期时间QCTime{get;set;}
公共字符串PcName{get;set;}
}

由于您试图按DateTime列进行分组,而该列仅以毫秒为单位变化,因此分组并没有按照您的预期进行。我修改了您的代码如下:

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(); 
        }
类标识
{
公共int id{get;set;}
公共日期时间QCTime{get;set;}
公共字符串PcName{get;set;}
公共字符串QCTimeForGroup
{
得到
{
if(QCTime.millisted 300&&QCTime.millisted 400&&QCTime.millisted x.QCTime).ThenBy(x=>x.PcName.ToList();//这里我按时间排序,然后是电脑名
var items1=listOfIdentity.OrderBy(x=>x.QCTime)。然后by(x=>x.PcName)。GroupBy(x=>x.PcName)。选择(grp=>grp.ToList()).ToList();//使用**Test1、Test2和Test3**(3个子列表)创建子列表
var items2=listOfIdentity.OrderBy(x=>x.QCTime).然后by(x=>x.PcName).GroupBy(x=>new{x.QCTime,x.PcName}).选择(grp=>grp.ToList()).ToList();//根据每个时间和pc名称创建子列表(9个子列表)
var items3=listOfIdentity.OrderBy(x=>x.QCTime)。然后是by(x=>x.PcName)
.GroupBy(x=>new{x.QCTimeForGroup,x.PcName})。选择(grp
=>grp.ToList()).ToList();
}

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

private void按钮\u单击(对象发送者,事件参数e)
{
List listOfIdentity=新列表()
{
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 sortedIdentity=listOfIdentity.OrderBy(x=>x.QCTime).ThenBy(x=>x.PcName).ToList();//这里我按时间排序,然后是电脑名
var result=sortedIndentity.Skip(1).Aggregate(新列表{new List{sortedIndentit
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);