使用lambda表达式按某些属性拆分对象组的列表

时间:2012-11-12 07:44:15

标签: c# linq list lambda .net-4.5

这是我的类型:

public class MyObj {
    public long Number { get; set; }
    public string Name { get; set; }
    public string Message { get; set; }
    public DateTime RegisterDate { get; set; }
}

所以我有一个Myobj的列表我需要将列表拆分为一些列表,其中NameNumber个对象的对象相等,有些东西就像{ {1}}和Name

假设此示例:

Number

所以我需要一个List<MyObj> MyObjects = new List<MyObj>{ new MyObj() { Number = 1, Name = "BMW", Message = "msg1", RegisterDate = DateTime.Now }, new MyObj() { Number = 1, Name = "BMW", Message = "msg2", RegisterDate = DateTime.Now }, new MyObj() { Number = 1, Name = "BMW", Message = "msg3", RegisterDate = DateTime.Now }, new MyObj() { Number = 1, Name = "Honda", Message = "msg11", RegisterDate = DateTime.Now }, new MyObj() { Number = 1, Name = "Honda", Message = "msg12", RegisterDate = DateTime.Now }, new MyObj() { Number = 2, Name = "BMW", Message = "msg22", RegisterDate = DateTime.Now }, new MyObj() { Number = 2, Name = "BMW", Message = "msg23", RegisterDate = DateTime.Now }, new MyObj() { Number = 2, Name = "BMW", Message = "msg24", RegisterDate = DateTime.Now }, new MyObj() { Number = 2, Name = "Honda", Message = "msg30", RegisterDate = DateTime.Now }, new MyObj() { Number = 3, Name = "BMW", Message = "msg41", RegisterDate = DateTime.Now }, new MyObj() { Number = 3, Name = "BMW", Message = "msg42", RegisterDate = DateTime.Now }, new MyObj() { Number = 3, Name = "Ford", Message = "msg51", RegisterDate = DateTime.Now }, new MyObj() { Number = 3, Name = "Ford", Message = "msg52", RegisterDate = DateTime.Now }, new MyObj() { Number = 3, Name = "Ford", Message = "msg53", RegisterDate = DateTime.Now } };

List<List<MyObj>>

结果是:

        List<MyObj> MyObjectwith1AndBMW = new List<MyObj> {
            new MyObj() { Number = 1, Name = "BMW", Message = "msg1", RegisterDate = DateTime.Now },
            new MyObj() { Number = 1, Name = "BMW", Message = "msg2", RegisterDate = DateTime.Now },
            new MyObj() { Number = 1, Name = "BMW", Message = "msg3", RegisterDate = DateTime.Now }
        };

        List<MyObj> MyObjectWith1AndHonda = new List<MyObj> {
            new MyObj() { Number = 1, Name = "Honda", Message = "msg11", RegisterDate = DateTime.Now },
            new MyObj() { Number = 1, Name = "Honda", Message = "msg12", RegisterDate = DateTime.Now }
        };

        List<MyObj> MyObjectWith2AndBMW = new List<MyObj> {
            new MyObj() { Number = 2, Name = "BMW", Message = "msg22", RegisterDate = DateTime.Now },
            new MyObj() { Number = 2, Name = "BMW", Message = "msg23", RegisterDate = DateTime.Now },
            new MyObj() { Number = 2, Name = "BMW", Message = "msg24", RegisterDate = DateTime.Now }
        };

        List<MyObj> MyObjectWith2AndHonda = new List<MyObj> {
            new MyObj() { Number = 2, Name = "Honda", Message = "msg30", RegisterDate = DateTime.Now }
        };

        List<MyObj> MyObjectwith3AndBMW = new List<MyObj> {
            new MyObj() { Number = 3, Name = "BMW", Message = "msg41", RegisterDate = DateTime.Now },
            new MyObj() { Number = 3, Name = "BMW", Message = "msg42", RegisterDate = DateTime.Now }
        };

        List<MyObj> MyObjectWith3AndFord = new List<MyObj> {
            new MyObj() { Number = 3, Name = "Ford", Message = "msg51", RegisterDate = DateTime.Now },
            new MyObj() { Number = 3, Name = "Ford", Message = "msg52", RegisterDate = DateTime.Now },
            new MyObj() { Number = 3, Name = "Ford", Message = "msg53", RegisterDate = DateTime.Now }
        };

那你的建议是什么?如何找到具有相同名称和相同数字的List<List<MyObj>> result = new List<List<MyObj>> { MyObjectwith1AndBMW, MyObjectWith1AndHonda, MyObjectWith2AndBMW, MyObjectWith2AndHonda, MyObjectwith3AndBMW, MyObjectWith3AndFord }; 列表?

2 个答案:

答案 0 :(得分:7)

  List<List<MyObj>> result = MyObjects.GroupBy(m => new { m.Name, m.Number })
                            .Select(g => g.ToList())
                            .ToList();

答案 1 :(得分:0)

var groups = (from row in MyObjects
              group row by new { row.Number, row.Name } into grp
              select grp.ToList()).ToList();

这是由List<List<MyObj>>.Number(一起)分组的.Name