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

wordpress pods插件搜索引擎优化师工资

wordpress pods插件,搜索引擎优化师工资,跨境电商是不是诈骗,网站建设公司愿景目录 一、示例代码 二、示例输出 三、注意雷点 四、全字段去重封装方法 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 List<Person>{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() 一般用于List<string>,List<int>这些值类型去重,而不涉及引用类型的字段比较。

people.Distinct().ToList()

        若需要全字段去重:1.使用DinstinctBy语法,加上所有字段。2.使用标题四的封装方法(反射实现全字段去重)。

四、全字段去重封装方法

1.封装

/// <summary>
/// 通用的全字段去重方法
/// </summary>
/// <returns></returns>
public static IEnumerable<T> DistinctByAllFields<T>(IEnumerable<T> 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 List<Person>{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></returns>public static IEnumerable<T> DistinctByAllFields<T>(IEnumerable<T> 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.hkea.cn/news/545711/

相关文章:

  • 网页制作软件属于什么软件类别简述seo的优化流程
  • 网站建设 公司新闻谷歌排名网站优化
  • 怎样做自己的vip解析网站佛山外贸seo
  • 我的网站在百度搜不到了seo是什么职业做什么的
  • 网站私信界面国外网站seo免费
  • wordpress mysql类惠州网站seo
  • 为什么做网站必须要用域名举出最新的网络营销的案例
  • 电子请柬网站开发百度竞价推广登录入口
  • 网站设计与推广国际时事新闻2022最新
  • 柬埔寨网站开发营销技巧和营销方法
  • 网站建立价格长沙网站外包公司
  • 王建设医生个人网站免费google账号注册入口
  • 免费自建手机网站搜索引擎优化的方法包括
  • 甘肃省建设工程安全质量监督管理局网站官网拉新项目官方一手平台
  • 做电影网站赚钱武汉新闻最新消息
  • 做网站没有成本的方法上海百度分公司电话
  • 寺庙网站建设百度ai人工智能
  • 完成公司网站建设下载关键词推广软件
  • wordpress如何关闭网站下载app
  • WordPress小程序二次修改石家庄seo排名外包
  • 做百度关键词网站厦门seo外包
  • 泉州seo-泉州网站建设公司谷歌关键词搜索工具
  • 组织部网站建设方案行业关键词分类
  • 上海黄浦 网站制作中国搜索引擎排名2021
  • 手机网站建设 cms营销技巧和营销方法
  • 平顶山做网站优化微博搜索引擎优化
  • 网站如何做品牌宣传海报每日舆情信息报送
  • 做论坛网站需要多大空间seo推广招聘
  • 中国建设银行网站软件不限次数观看视频的app
  • 网站开发建设的步骤win11优化大师