Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read text file into dictionary without duplicates

Tags:

c#

linq

I am reading a text file into a dictionary using a short Linq expression

string[] lines = File.ReadAllLines(path);
var dictionary = lines.Select(line => line.Split(';')).ToDictionary(keyValue => keyValue[0], bits => bits[1]);

This works just fine as long as I dont have duplicate keys in my text file. Is there a short way to filter those without going the long way and iterating over the lines[] array?

like image 590
silent Avatar asked May 21 '14 09:05

silent


1 Answers

You can use GroupBy first:

var dictionary = lines.Select(line => line.Split(';'))
    .GroupBy(arr => arr[0])
    .ToDictionary(g => g.Key, g => g.First()[1]);

This selects the first element of each duplicate, if that's not desired you have to change g.First accordingly. You could for example separate the values with comma:

var dictionary = lines.Select(line => line.Split(';'))
    .GroupBy(arr => arr[0])
    .ToDictionary(g => g.Key, g => string.Join(",", g.Select(arr=> arr[1])));
like image 153
Tim Schmelter Avatar answered Sep 20 '22 19:09

Tim Schmelter