Code:List中根据某个字段找出重复项,取某条特定的记录,并和另外的重复项做求和,并删除另一条记录

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqFordistinct
{
   public class Plants
   {
      public int Age;
      public string name;
      public string Status;
   }
   
   public class Program
   {

      public static void GroupByKeyForDictionary()
      {
         Dictionary<int, string> plants = new Dictionary<int, string>()
         {
            {1, "Speckled Alder"},
            {2, "Apple of Sodom"},
            {3, "Hairy Bittercress"},
            {4, "Pennsylvania Blackberry"},
            {5, "Apple of Sodom"},
            {6, "Water Birch"},
            {7, "Meadow Cabbage"},
            {8, "Water Birch"},
            {9, "test"},
            {10, "test"},
         };
         Console.WriteLine("dictionary elements........ ");
         //loop dictionary all elements  
         foreach (KeyValuePair<int, string> pair in plants)
         {
            Console.WriteLine(pair.Key + "....." + pair.Value );
         }
         foreach (var pair in plants)
         {
            Console.WriteLine(pair.Key + "....." + pair.Value );
         }
         //find dictionary duplicate values.
         var duplicateValues = plants.GroupBy(x => x.Value).Where(x => x.Count() > 1);

         Console.WriteLine("dictionary duplicate values..........");

         //loop dictionary duplicate values only           
         foreach (var item in duplicateValues)
         {
            Console.WriteLine(item.Key + ".....");
         }
      }


      //使用场景,对于一个List,根据某个字段找出重复项,取某条特定的记录,并和另外的重复项做求和,并删除另一条记录
      public static void GroupByKeyForClass()
      {
         // Dictionary<int, string> plants = new Dictionary<int, string>()
         IList<Plants> plants = new List<Plants>();
         plants.Add(new Plants() { Age = 1, name = "Speckled Alder", Status = "Man" });
         plants.Add(new Plants() { Age = 2, name = "Apple of Sodom", Status = "Man" });
         plants.Add(new Plants() { Age = 3, name = "Apple of Sodom", Status = "Woman" });
         plants.Add(new Plants() { Age = 4, name = "Pennsylvania Blackberry", Status = "Man" });
         plants.Add(new Plants() { Age = 5, name = "Pennsylvania Blackberry", Status = "Woman" });
         plants.Add(new Plants() { Age = 6, name = "Water Birch" });
         plants.Add(new Plants() { Age = 7, name = "Hairy Bittercress", Status = "Man" });
         plants.Add(new Plants() { Age = 8, name = "Hairy Bittercress", Status = "Woman" });
         plants.Add(new Plants() { Age = 9, name = "Meadow Cabbage", Status = "Man" });
         plants.Add(new Plants() { Age = 10, name = "Meadow Cabbage", Status = "Woman" });
         plants.Add(new Plants() { Age = 11, name = "Test" });
         
         Console.WriteLine("dictionary elements........................ ");
         foreach (var pair in plants)
         {
            Console.WriteLine(pair.Age + "....." + pair.name + "..........");
         }
         //find dictionary duplicate values.
         var duplicateValues = plants.GroupBy(x => x.name).Where(x => x.Count() > 1);
         Console.WriteLine("dictionary duplicate values..........");
         //loop dictionary duplicate values only           
         foreach (var item in duplicateValues)
         {
            Console.WriteLine(item.Key );
         }
         for (int i = plants.Count - 1; i >= 0; i--)
         {
            for (int j = 0; j < duplicateValues.Count(); j++)
            {
               if (plants[i].name == duplicateValues.ToArray()[j].Key && plants[i].Status == "Man")
               {
                  var test = plants.ToList().Find(p => p.name == plants[i].name && p.Status == "Woman");
                  plants[i].Age += test.Age;
               }
            }
         }
         //loop dictionary duplicate values only           
         foreach (var item in plants)
         {
            Console.WriteLine(item.Age + "....." + item.name + "....." + item.Status );
         }
         for (int i = plants.Count - 1; i >= 0; i--)
         {
            for (int j = 0; j < duplicateValues.Count(); j++)
            {
               if (plants[i].name == duplicateValues.ToArray()[j].Key)
               {
                  plants.RemoveAt(i);
               }
            }
         }
         //loop dictionary duplicate values only           
         foreach (var item in plants)
         {
            Console.WriteLine(item.Age + "....." + item.name + "....." + item.Status );
         }
         Console.WriteLine("........................................");
      }

      static void Main(string[] args)
      {
         GroupByKeyForClass();
         GroupByKeyForDictionary();
         Console.ReadLine();

      }
   }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值