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>,
上面的例子将组合成三个
当当网
天猫
京东
该怎么实现?
直接 分组一下即可。
var query=goodsList.GroupBy(g=>g.GoodsForm);
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("**********************************"); } }
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); }
楼上正解,goodsList.GroupBy(g=>g.GoodsForm)将得到一个IEnumerable<string,List<Goods>>集合
var query=goodsList.Where(g=>g.GoodsForm==“京东”); 既可以筛选出 其中是京东的商品