解决方案:40分
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { List<TempClass> list = new List<TempClass>(); List<String[]> ls = new List<String[]>(); //读取csv文件 StreamReader fileReader = new StreamReader(@"C:\Users\Administrator\Desktop\test.csv"); string strLine=""; //读取csv的行并存储到泛型列表中 while (strLine != null) { strLine = fileReader.ReadLine(); if (strLine != null && strLine.Length>0) { TempClass t = new TempClass() { ID = strLine.Split("","")[0], OriginalStr = strLine }; list.Add(t); } } fileReader.Close(); Console.WriteLine("before distinct↓↓↓↓"); Console.WriteLine(list.Count); //对泛型进行去重 var tempList = list.Distinct(new TempIDComparer()); Console.WriteLine("after distinct↓↓↓↓"); Console.WriteLine(tempList.ToList().Count); //将去重后的泛型集合写入新的csv文件 StreamWriter fileWriter=new StreamWriter(@"C:\Users\Administrator\Desktop\result.csv",false,Encoding.Default); foreach(var item in tempList.ToList()) { fileWriter.WriteLine(item.OriginalStr); } fileWriter.Flush(); fileWriter.Close(); Console.ReadKey(); } } //将需要比较的列及原始数据行存入对象中 public class TempClass { public string ID { get; set; } public string OriginalStr { get; set; } } //实现compare类 用于distinct方法 public class TempIDComparer : IEqualityComparer<TempClass> { public bool Equals(TempClass x, TempClass y) { if (x == null) return y == null; return x.ID == y.ID; } public int GetHashCode(TempClass obj) { if (obj == null) return 0; return obj.ID.GetHashCode(); } } }