Search code examples
c#linq

Read text file into dictionary without duplicates


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?


Solution

  • 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])));