中国建设银行上海分行信息网站,备案 网站首页网址,wordpress 勾子,成都高新seo目录
一、示例代码
二、示例输出
三、注意雷点
四、全字段去重封装方法
1.封装
2.示例 一、示例代码
using System;
using System.Collections.Generic;
using System.Linq;public class Program
{public static void Main(){// 创建一些示例实体对象var people new Li…目录
一、示例代码
二、示例输出
三、注意雷点
四、全字段去重封装方法
1.封装
2.示例 一、示例代码
using System;
using System.Collections.Generic;
using System.Linq;public class Program
{public static void Main(){// 创建一些示例实体对象var people new ListPerson{new Person { Name Alice, Age 30, City New York },new Person { Name Bob, Age 25, City Los Angeles },new Person { Name Alice, Age 30, City New York },//重复的new Person { Name Charlie, Age 35, City Chicago },new Person { Name Alice, Age 28, City San Francisco }};// 1. 单字段去重var uniqueNameFields people.DistinctBy(p p.Name).ToList();Console.WriteLine(指定字段Name去重结果,重复则保留第一条:);foreach (var person in uniqueNameFields){Console.WriteLine($Name: {person.Name}, Age: {person.Age}, City: {person.City});}// 2. 多字段去重var uniqueNameAgeFields people.DistinctBy(p new { p.Name, p.Age }).ToList();Console.WriteLine(\n指定字段Name, Age去重结果,重复则保留第一条:);foreach (var person in uniqueNameAgeFields){Console.WriteLine($Name: {person.Name}, Age: {person.Age}, City: {person.City});}//3.全字段去重// 通过 GroupBy 按 Name 和 Age 字段去重var uniquePeople people.DistinctBy(p new { p.Name, p.Age, p.City }).ToList();Console.WriteLine(\n全字段去重:);foreach (var person in uniquePeople){Console.WriteLine($Name: {person.Name}, Age: {person.Age}, City: {person.City});}}
}public class Person
{public string Name { get; set; }public int Age { get; set; }public string City { get; set; }
}二、示例输出
指定字段Name去重结果,重复则保留第一条:
Name: Alice, Age: 30, City: New York
Name: Bob, Age: 25, City: Los Angeles
Name: Charlie, Age: 35, City: Chicago指定字段Name, Age去重结果,重复则保留第一条:
Name: Alice, Age: 30, City: New York
Name: Bob, Age: 25, City: Los Angeles
Name: Charlie, Age: 35, City: Chicago
Name: Alice, Age: 28, City: San Francisco全字段去重:
Name: Alice, Age: 30, City: New York
Name: Bob, Age: 25, City: Los Angeles
Name: Charlie, Age: 35, City: Chicago
Name: Alice, Age: 28, City: San Francisco
三、注意雷点 以下代码不能完成全字段去重因为people是引用类型Distinct() 一般用于Liststring,Listint这些值类型去重而不涉及引用类型的字段比较。 people.Distinct().ToList() 若需要全字段去重1.使用DinstinctBy语法加上所有字段。2.使用标题四的封装方法反射实现全字段去重。
四、全字段去重封装方法
1.封装
/// summary
/// 通用的全字段去重方法
/// /summary
/// returns/returns
public static IEnumerableT DistinctByAllFieldsT(IEnumerableT items)
{// 获取 T 类型的所有字段值var properties typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);return items.GroupBy(item string.Join(,, properties.Select(p p.GetValue(item)))) // 按所有字段值连接生成唯一标识符.Select(group group.First()); // 取每组的第一个元素
}
2.示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;public class Program
{public static void Main(){// 创建一些示例实体对象var people new ListPerson{new Person { Name Alice, Age 30, City New York },new Person { Name Bob, Age 25, City Los Angeles },new Person { Name Alice, Age 30, City New York },new Person { Name Charlie, Age 35, City Chicago },new Person { Name Alice, Age 28, City San Francisco }};// 调用封装的去重方法var uniquePeople DistinctByAllFields(people).ToList();Console.WriteLine(根据所有字段去重的结果:);foreach (var person in uniquePeople){Console.WriteLine($Name: {person.Name}, Age: {person.Age}, City: {person.City});}}/// summary/// 通用的全字段去重方法/// /summary/// returns/returnspublic static IEnumerableT DistinctByAllFieldsT(IEnumerableT items){// 获取 T 类型的所有字段值var properties typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);return items.GroupBy(item string.Join(,, properties.Select(p p.GetValue(item)))) // 按所有字段值连接生成唯一标识符.Select(group group.First()); // 取每组的第一个元素}
}public class Person
{public string Name { get; set; }public int Age { get; set; }public string City { get; set; }
}