C# IList,List,IEnumber用法
c#删除 list中的元素和怎么去除空元素 https://www.cnblogs.com/shy1766IT/p/5186363.html
for (int i = list.Count - 1; i >= 0; i--)
{
if (list[i].NO == item.NO)
{
list.RemoveAt(i);
}
}
public void RemoveItemFromList(ref List <A> list, A item)
{
List <A> tempList = new List <A>();
foreach (A a in list)
{
if (a.NO != item.NO && !tempList.Contains(a))
tempList.Add(a);
}
list = tempList;
}
如果支除空元素,可以使用Split参数StringSplitOptions.RemoveEmptyEntries去实现
用法:
string test = "程$晓$";
使用:string[] temp = test.Split(new string[] { "$" }, StringSplitOptions.RemoveEmptyEntries);
输出结果:数组长度为2 temp[0]="程" temp[1]="晓";
使用:string[] temp = test.Split(new string[] { "$" }, StringSplitOptions.None);或string[] temp = test.Split('$');
输出结果:数组长度为3 temp[0]="程" temp[1]="晓" temp[2]="";
string[] inputpids = productIds.IndexOf(',') > 0 ? productIds.Split(',').Distinct().ToArray() : new string[] { productIds };
int[] outputpids = Array.ConvertAll<string, int>(inputpids, delegate(string s)
{
var val = 0;
int.TryParse(s, out val);
return val;
});
req.ProductIds = outputpids.Where(c=>c>0).ToList();
C# List集合分组去重并保留最新的数据
class Program
{
static void Main(string[] args)
{
List<Person> list = new List<Person>
{
new Person
{
id = 1,
name = "李小龙",
date = "2021-10-21"
},
new Person
{
id = 1,
name = "李小龙",
date = "2021-11-21"
},
new Person
{
id = 2,
name = "李世超",
date = "2021-10-21"
}
};
//分组去重保留最新数据
List<Person> newList = list.GroupBy(P => P.id).Select(P => P.OrderByDescending(P => P.date).First()).ToList();
foreach (var item in newList)
{
Console.WriteLine($"人员编号:{item.id} 人员名称:{item.name} 日期:{item.date}");
}
Console.ReadKey();
}
public class Person
{
/// <summary>
/// 人员编号
/// </summary>
public int id { get; set; }
/// <summary>
/// 人员名称
/// </summary>
public string name { get; set; }
/// <summary>
/// 日期
/// </summary>
public string date { get; set; }
}
}
“fool me once,shame on you. fool me twice, shame on me.”,翻译过来的意思是“愚弄我一次,是你坏;愚弄我两次,是我蠢”。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具