首页 新闻 会员 周边 捐助

根据不同商品来源拆分List<>,根据商品来源不同组成新的List<>?求解

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

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);

怎么将goodsList拆分成三个goodsList
goodsList1包含京东的商品
goodsList2包含天猫的商品
goodsList3包含当当的商品

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

直接 分组一下即可。

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

奖励园豆:5
Qlin | 老鸟四级 |园豆:2403 | 2013-01-28 08:43
其他回答(1)
0

dic<string,List<>>

string 是来源

chenping2008 | 园豆:9836 (大侠五级) | 2013-01-27 14:01
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);

            }
支持(0) 反对(0) chenping2008 | 园豆:9836 (大侠五级) | 2013-01-27 18:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册