list中存放的是
imei 865209031060925 recivedTime 2017/10/16 14:33:52
imei 865209031060925 recivedTIme 2017/10/12 16:57:27
这种数据,imei后面紧跟对应时间,我想将imei相同的时间放到一起 如 imei 865209031060925 2017/10/16 14:33:52 2017/10/12 16:57:27,该怎做?
分组把要的IMEI拿出来之后,再把相关的时间列表塞进去,至于能不能一句话直接塞进去,这种处理大概没有吧?至少我还写不出来
public class TestObj { public string IMEI { get; set; } public DateTime CreateTime { get; set; } } //结果对象 public class TestObjDTO { public string IMEI { get; set; } public List<DateTime> CreateTimeList { get; set; } }
#region List分组 List<TestObj> list2 = new List<TestObj>(); for (int i = 0; i < 5; i++) { TestObj temp = new TestObj() { CreateTime = DateTime.Now.AddDays(i), IMEI = (i % 2).ToString() }; list2.Add(temp); } List<TestObjDTO> listDTO = list2.GroupBy(u => u.IMEI).Select(u => new TestObjDTO() { IMEI = u.FirstOrDefault().IMEI }).ToList(); foreach (var item in listDTO) { item.CreateTimeList = list2.Where(u => u.IMEI == item.IMEI).Select(u => u.CreateTime).ToList(); } Console.WriteLine(SerializationHelper.JsonSerialize(listDTO)); #endregion
这是JSON化显示了
class Program { static void Main(string[] args) { var array = new List<Phone> { new Phone { IMEI = "865209031060921", RecivedTime = "2017/10/11 14:33:52" }, new Phone { IMEI = "865209031060921", RecivedTime = "2017/10/12 14:33:52" }, new Phone { IMEI = "865209031060922", RecivedTime = "2017/10/13 14:33:52" }, new Phone { IMEI = "865209031060922", RecivedTime = "2017/10/14 14:33:52" }, }; array.GroupBy(m => m.IMEI).Select(m => new { IMEI = m.Key, Times = String.Join(",", m.Select(item => item.RecivedTime)) }); Console.ReadKey(); } } class Phone { public String IMEI { get; set; } public String RecivedTime { get; set; } }
class Phone { public String IMEI { get; set; } public String RecivedTime { get; set; } } class Phone1 { public string IMEI { get; set; } public string ReceivedTime1 { get; set; } public string RecievedTime2 { get; set; } }
var array = new List<Phone> { new Phone { IMEI = "865209031060921", RecivedTime = "2017/10/11 14:33:52" }, new Phone { IMEI = "865209031060921", RecivedTime = "2017/10/12 14:33:52" }, new Phone { IMEI = "865209031060922", RecivedTime = "2017/10/13 14:33:52" }, new Phone { IMEI = "865209031060922", RecivedTime = "2017/10/14 14:33:52" } }; array = array.GroupBy(m => m.IMEI).Select(m => new Phone { IMEI = m.Key, RecivedTime = string.Join(",", m.Select(item => item.RecivedTime)) }).ToList(); //IMEI=865209031060921 ReceivedTime1=2017/10/11 14:33:52 RecievedTime2=2017/10/12 14:33:52 //这种形式能实现吗?谢谢
@Mr.Gethin:
class Program { static void Main(string[] args) { var array = new List<Phone> { new Phone { IMEI = "865209031060921", RecivedTime = "2017/10/11 14:33:52" }, new Phone { IMEI = "865209031060921", RecivedTime = "2017/10/12 14:33:52" }, new Phone { IMEI = "865209031060922", RecivedTime = "2017/10/13 14:33:52" }, new Phone { IMEI = "865209031060922", RecivedTime = "2017/10/14 14:33:52" }, }; array.GroupBy(m => m.IMEI).Select(m => { var sb = new StringBuilder(); sb.AppendFormat("IMEI={0} ", m.Key); for (var i = 0; i < m.Count(); i++) { var item = m.ElementAt(i); sb.AppendFormat("RecivedTime{0}={1} ", i + 1, item.RecivedTime); } return sb.ToString(); }).ToList().ForEach(m => { Console.WriteLine(m); }); Console.ReadKey(); } } class Phone { public String IMEI { get; set; } public String RecivedTime { get; set; } }
@写代码的小2B: 如果是我上面说的两个类的话不能实现吗?
把list放map里面,id存key值,map取值,相同的话,在value里面拼接一下就好了