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?
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])));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With