silent
silent

Reputation: 16128

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?

Upvotes: 5

Views: 1721

Answers (2)

dabinsi
dabinsi

Reputation: 806

@Tim Schmelter. For my specific case, removing blank lines, I used your code + .Where():

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

I'm wondering if there is a better way. Thanks

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460098

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

Upvotes: 11

Related Questions