首页 新闻 搜索 专区 学院

从List<T>找出相同商品来源的商品,根据相同的商品来源组合成新的List<T>,如何实现

0
[已解决问题] 解决于 2013-04-01 14:13

GoodsNumber 商品编号、Title商品名称、GoodsForm商品源
List<Goods> goodsList = new List<Goods>();
Goods good1 = new Goods();
good1.GoodsNumber = 1001;
good1.Title = "iphone4";
good1.GoodsForm = "京东";
goodsList.Add(good1);
Goods good2 = new Goods();
good2.GoodsNumber = 1002;
good2.Title = "HTC G11";
good2.GoodsForm = "京东";
goodsList.Add(good2);
Goods good3 = new Goods();
good3.GoodsNumber = 1003;
good3.Title = "三星NOTE2";
good3.GoodsForm = "天猫";
goodsList.Add(good3);
Goods good4 = new Goods();
good4.GoodsNumber = 1004;
good4.Title = "联想乐phone";
good4.GoodsForm = "天猫";
goodsList.Add(good4);
Goods good5 = new Goods();
good4.GoodsNumber = 1005;
good4.Title = "小鬼当家漫画";
good4.GoodsForm = "当当网";
goodsList.Add(good5);

找出相同商品来源的商品,根据相同的商品来源组合成新的List<Goods>,
上面的例子将组合成三个
当当网 
天猫
京东
该怎么实现?

猪头猪脑的主页 猪头猪脑 | 初学一级 | 园豆:6
提问于:2013-01-27 15:20
< >
分享
最佳答案
0

直接 分组一下即可。

var query=goodsList.GroupBy(g=>g.GoodsForm);

奖励园豆:5
Qlin | 老鸟四级 |园豆:2403 | 2013-01-28 08:44
其他回答(4)
0
    public class Goods 
    {
        public int GoodsNumber;

        public string Title;
        public string GoodsForm;
    }


        static void Main(string[] args)
        {
            List<Goods> goodsList = new List<Goods>();
            Goods good1 = new Goods();
            good1.GoodsNumber = 1001;
            good1.Title = "iphone4";
            good1.GoodsForm = "京东";
            goodsList.Add(good1);

            Goods good2 = new Goods();
            good2.GoodsNumber = 1002;
            good2.Title = "HTC G11";
            good2.GoodsForm = "京东";
            goodsList.Add(good2);


            Goods good3 = new Goods();
            good3.GoodsNumber = 1003;
            good3.Title = "三星NOTE2";
            good3.GoodsForm = "天猫";
            goodsList.Add(good3);


            Goods good4 = new Goods();
            good4.GoodsNumber = 1004;
            good4.Title = "联想乐phone";
            good4.GoodsForm = "天猫";
            goodsList.Add(good4);


            Goods good5 = new Goods();
            good5.GoodsNumber = 1005;
            good5.Title = "小鬼当家漫画";
            good5.GoodsForm = "当当网";
            goodsList.Add(good5);


            //复制一份
            List<Goods> list2=new List<Goods>();
            goodsList.ForEach(i => list2.Add(i));


            //主要是为得到键值
            List<Goods> listKeyA = goodsList.GroupBy(p => p.GoodsForm)
                .Select(p => new Goods { GoodsForm = p.Key, GoodsNumber = p.FirstOrDefault().GoodsNumber,Title=p.FirstOrDefault().Title }).ToList();







            //得到分组后的数据
            List<List<Goods>> listTotal = new List<List<Goods>>();




            for (int i = 0; i < listKeyA.Count; i++)
            {
                List<Goods> listABC = new List<Goods>();
                for (int j = 0; j < list2.Count; j++)
                {
                    if (list2[j].GoodsForm.Equals(listKeyA[i].GoodsForm))
                    {
                        listABC.Add(list2[j]);

                    }
                }

                listTotal.Add(listABC);

            }


               //这里就是
    //分好组的List
            foreach (var item in listTotal)
            {
                foreach (var item1 in item)
                {
                    Console.WriteLine(item1.GoodsForm);
                }

                Console.WriteLine("**********************************");
            }


        }        
田麦成 | 园豆:1988 (小虾三级) | 2013-01-27 17:13
0
List<Goods> goodsList = new List<Goods>();

            Goods good1 = new Goods();
            good1.GoodsNumber = 1001;
            good1.Title = "iphone4";
            good1.GoodsForm = "京东";
            goodsList.Add(good1);

            Goods good2 = new Goods();
            good2.GoodsNumber = 1002;
            good2.Title = "HTC G11";
            good2.GoodsForm = "京东";
            goodsList.Add(good2);

            Goods good3 = new Goods();
            good3.GoodsNumber = 1003;
            good3.Title = "三星NOTE2";
            good3.GoodsForm = "天猫";
            goodsList.Add(good3);

            Goods good4 = new Goods();
            good4.GoodsNumber = 1004;
            good4.Title = "联想乐phone";
            good4.GoodsForm = "天猫";
            goodsList.Add(good4);

            Goods good5 = new Goods();
            good5.GoodsNumber = 1005;
            good5.Title = "小鬼当家漫画";
            good5.GoodsForm = "当当网";
            goodsList.Add(good5);


            Dictionary<string, List<Goods>> result = new Dictionary<string, List<Goods>>();


            foreach (var item in goodsList)
            {
                if (!(result.ContainsKey(item.GoodsForm)))
                {
                    result.Add(item.GoodsForm, new List<Goods>());
                }
                result[item.GoodsForm].Add(item);

            }
chenping2008 | 园豆:9836 (大侠五级) | 2013-01-27 18:07
0

楼上正解,goodsList.GroupBy(g=>g.GoodsForm)将得到一个IEnumerable<string,List<Goods>>集合

Rich.T | 园豆:3440 (老鸟四级) | 2013-01-28 14:12
0

var query=goodsList.Where(g=>g.GoodsForm==“京东”); 既可以筛选出  其中是京东的商品

_Esc | 园豆:204 (菜鸟二级) | 2013-01-28 15:46
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册