根据第一列从CSV文件中获取唯一列表 [英] Get Distinct List from csv file based on first column

查看:74
本文介绍了根据第一列从CSV文件中获取唯一列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下从csv文件中获取的st的列表...

I have the following list of stings taken from a csv file...

List<string> listOfRecords;

每行都是列表中的一个字符串...

Each line is a string in the list...

one,bob,black
two,steve,smith
three,bill,brown
one,jill,brown
one,sue,smith

我想根据每行的第一个值删除重复项.导致...

I would like to remove duplicates based on the first value on each line. Resulting in...

one,bob,black
two,steve,smith
three,bill,brown

我认为代码看起来像....

I thought the code would look something like....

distinctlist = Select listOfRecords.split(',')[0].distinct

这显然是错误的,但是我想避免创建列表列表,而是这样做.认为linq会更简单.

this is obviously wrong but I wanted to avoid making a list of lists and doing it that way. Thinking linq would be simpler.

我在这里可以找到的所有帖子似乎都很复杂,或者没有解决我的问题的细节.任何帮助将不胜感激...

All the posts I can find on here seem quite complex or do not address the specifics of my question. Any help would be greatly appreciated...

推荐答案

简单的GroupBy:

var distinctByFirstColumn = listOfRecords
  .GroupBy(x => x.Split(',')[0])
  .Select(x => x.First());

这篇关于根据第一列从CSV文件中获取唯一列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆