欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C#在同一List中判断是否有相同的产品ID和产品数量

程序员文章站 2022-07-07 09:45:22
...

C#在同一List中判断是否有相同的产品ID和产品数量

方法一

// 新建一个空的字典集合
Dictionary<string,int>d=new Dictionary<string,int>();
//循环要比较的那个集合
foreach (var item in ProductList)
{
 string key=item.ProductId+","+item.ProductCount;
 //对字典集合和集合的产品id和产品名称进行比较匹配
 if(d.Keys.Contains(key))
 {
   Console.WriteLine("集合中产品id相同且产品数量相同的产品id为{0}{1})",item.ProductId,item.ProductName);
 }
 else
 {
 d.add(key,0);
 }
}

方法二():

var p=PruductList.GroupBy(qp=>new {qp.ProductId,qp.ProductCount}).ToList();
if(p.count<ProductList.count)
{
Consolel.Writelint("该集合中存在产品id且产品数量都一致的产品";
}
相关标签: c#