我想的是csv文件不是保存到了IEnumerable<object[]{string[]}>类型数据中了吗,可不可以不那么麻烦,直接利用Linq进行去重可以吗
时间: 2023-12-26 18:03:50 浏览: 140
非常抱歉,我之前的回答可能有点复杂了。确实,您可以使用 LINQ 来更简单地进行去重操作。以下是一个示例代码:
```csharp
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Program
{
static void Main(string[] args)
{
string filePath = "path/to/your/csv/file.csv";
IEnumerable<string[]> data = ExtractDataFromCSV(filePath);
data = RemoveDuplicates(data, 10); // 第11列的索引为10
// 打印去重后的数据
foreach (var row in data)
{
Console.WriteLine(string.Join(", ", row));
}
}
static IEnumerable<string[]> ExtractDataFromCSV(string filePath)
{
using (var reader = new StreamReader(filePath))
{
while (!reader.EndOfStream)
{
yield return reader.ReadLine().Split(',');
}
}
}
static IEnumerable<string[]> RemoveDuplicates(IEnumerable<string[]> data, int columnIndex)
{
return data.GroupBy(row => row[columnIndex])
.Select(group => group.First());
}
}
```
这个代码更简洁,直接通过 LINQ 的 `GroupBy` 方法进行分组,并选择每个组中的第一行,即完成了去重操作。请将 `path/to/your/csv/file.csv` 替换为您的实际 CSV 文件路径。
这样,您就可以直接利用 LINQ 进行去重,无需使用额外的数据结构。每行数据仍然以 `string[]` 数组的形式存储在 `IEnumerable<string[]>` 变量中。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)