这是我的类型:
public class MyObj {
public long Number { get; set; }
public string Name { get; set; }
public string Message { get; set; }
public DateTime RegisterDate { get; set; }
}
所以我有一个列表,Myobj
我需要将列表拆分为一些列表,其中对象的对象Name
和Number
对象的对象相等,例如按Name
and分组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
具有相同名称和相同数字的列表?