首页新闻找找看学习计划

如何将list中ID值相同的数据合成一条

0
悬赏园豆:5 [已解决问题] 解决于 2017-10-26 11:37

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,该怎做?

Mr.Gethin的主页 Mr.Gethin | 菜鸟二级 | 园豆:218
提问于:2017-10-17 10:24
< >
分享
最佳答案
0

分组把要的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化显示了

收获园豆:3
Danlis | 菜鸟二级 |园豆:226 | 2017-10-17 16:53
其他回答(2)
0
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; }
}
收获园豆:2
写代码的小2B | 园豆:4346 (老鸟四级) | 2017-10-17 10:56
        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
           //这种形式能实现吗?谢谢
支持(0) 反对(0) Mr.Gethin | 园豆:218 (菜鸟二级) | 2017-10-17 11:51

@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; }
}
支持(0) 反对(0) 写代码的小2B | 园豆:4346 (老鸟四级) | 2017-10-17 12:12

@写代码的小2B: 如果是我上面说的两个类的话不能实现吗?

支持(0) 反对(0) Mr.Gethin | 园豆:218 (菜鸟二级) | 2017-10-17 13:49
0

把list放map里面,id存key值,map取值,相同的话,在value里面拼接一下就好了

大宇007 | 园豆:329 (菜鸟二级) | 2017-10-17 18:01
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册