3

这是我的类型:

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对象的对象相等,例如按Nameand分组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
 };

那么你的建议是什么?如何找到MyObj具有相同名称和相同数字的列表?

4

2 回答 2

7
  List<List<MyObj>> result = MyObjects.GroupBy(m => new { m.Name, m.Number })
                            .Select(g => g.ToList())
                            .ToList();
于 2012-11-12T07:49:59.477 回答
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

于 2012-11-12T07:50:23.067 回答