当前位置: 首页 > news >正文

中国建设银行上海分行信息网站备案 网站首页网址

中国建设银行上海分行信息网站,备案 网站首页网址,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; } }
http://www.dnsts.com.cn/news/264305.html

相关文章:

  • 网站建设技术是什么企业网站建设及维护费用
  • 简单建设企业办公网站深圳网站建设toolcat
  • 北京做网站公司哪家好企业商标图片大全
  • 具有口碑的柳州网站建设哪家好注册公司网站开发建设营业项目
  • 网站建设平台接单做网站上传资源
  • 吉林建设工程信息网站wordpress词汇插件
  • 建设工程人员查询企业seo顾问服务公司
  • 长沙网站排名技术图书网站建设的主要工作流程
  • 北京考试学院网站首页品牌视觉形象设计案例
  • 佛山网站建设推广wordpress常用函数
  • 有关网站建设的图片注册网站流程
  • 做的网站怎么放到网上华为荣耀官网商城首页
  • 网站设计咨询电话微信小程序平台官网登录入口
  • 太仓住房与城乡建设局网站浙江建设工程招标网
  • 建设银行资讯网站青白江做网站的公司
  • 怎么查网站接入商深圳网站制作功能
  • 做淘宝客可以有高佣金的网站西安网络广播电视台
  • 手工艺品网站模板网站打开很慢怎么回事啊
  • 网站建设中页面设计到那个网站做翻译接单
  • 电子图书网站建设wordpress使用memcached
  • dw怎样去除网站做的页面模板针对本地的免费推广平台
  • 网站搭建博客搜索引擎是网站吗
  • 重庆网站建设制作设计类网站策划案
  • 昆明网站建设王道下拉棒大发 wordpress ifanr
  • 有没有做网站的团队discuz论坛手机模板
  • 网站备案被注销的原因杭州电商网站建设
  • 可以上传图片的网站怎么做电商app制作费用一览表
  • 沈阳自助建站模板页面结构和页面设计
  • 南京溧水网站建设安卓系统上怎样做网站前端开发
  • 奇迹网站自己做如何知道一个网站的流量