我现在有这么一个需求,list里面存的是实体,我现在需要根据这个list中实体的一个属性,将此list截成若干个list,存在以此list为元素的一个大list里,这个需求应该怎么做,听着比较绕,若是直接循环取得话比较麻烦,数据量也挺大,有没有好一点的方法,大家讨论讨论,顶者有分,分不够我再追加

解决方案 »

  1.   

    你当初添加时就该分开存到不同的列表中,如果现在这样,只有循环了,List中不是还提供一个find方法吗
      

  2.   

    楼上说的也是,不过find本质上应该也是便利循环~
      

  3.   

    添加的时候比较麻烦,而且这个实体也很复杂,不是和数据库对应的,改起来比较麻烦,find只是返回第一个元素,达不到需求呀
      

  4.   

    用数据字典Dictionary<key,list> d = new Dictionary<key, list>();
      

  5.   

    要是这样的话,你应该用dictionary来存储的,键值对的方式,查找起来也LIST好
      

  6.   

    List<List<entityModel>> list =new List<List<entityModel>>();
    List<entityModel> allList;
    list.Add(allList.Where(m=>条件).ToList());
      

  7.   

    Linq的GroupBy...如果是2.0,应该在数据源解决,实体哪儿来的?在数据源group by...
      

  8.   

    参考:        class Test
            {
                public int Prop { get; set; }
                public string Name { get; set; }
            }            List<Test> list = new List<Test> 
                {
                    new Test{ Prop=1,Name="1"},
                    new Test{ Prop=2,Name="2"},
                    new Test{ Prop=1,Name="3"},
                    new Test{ Prop=4,Name="4"},
                    new Test{ Prop=3,Name="5"},
                    new Test{ Prop=2,Name="6"},
                    new Test{ Prop=1,Name="7"},
                    new Test{ Prop=4,Name="8"}
                };
                //上面是初始化测试数据,不用管,重点看下面
                List<List<Test>> list2 = new List<List<Test>>();
                while (list.Count > 0)
                {
                    int prop = list[0].Prop;
                    List<Test> listTemp = list.FindAll(delegate(Test t) { return t.Prop == prop; });
                    list2.Add(listTemp);
                    list.RemoveAll(delegate(Test t) { return t.Prop == prop; });
                }
                foreach (List<Test> l in list2)
                {
                    foreach (Test t in l)
                    {
                        Console.Write("{0} ", t.Name);
                    }
                    Console.WriteLine();
                }/*
    输出:
    1 3 7
    2 6
    4 8
    5
    */