概述
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();
}
}
}
最后
以上就是英俊枕头为你收集整理的Code:List中根据某个字段找出重复项,取某条特定的记录,并和另外的重复项做求和,并删除另一条记录的全部内容,希望文章能够帮你解决Code:List中根据某个字段找出重复项,取某条特定的记录,并和另外的重复项做求和,并删除另一条记录所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复