Mapping .csv rows to a Dictionary I have a .csv file in which each row represents an association between a software application and a web server. My goal is to fetch a List&lt;Server&gt; by application, so I'm mapping the .csv to a Dictionary&lt;string, List&lt;Server&gt;&gt;. I'm using CsvHelper to read the .csv file and map it to objects. 

.csv format:

Family | Environment | Name   | Application
01     | Dev         | WEBD01 | application1
01     | Production  | WEBP01 | application1
02     | Dev         | WEBD02 | application2


Server class:

public class Server
{
    public string Name { get; set; }
    public string Family { get; set; }
    public string Environment { get; set; }
}


Mapping:

protected override void RefreshData()
{        
    // _serverDictionary is a class-level Dictionary&lt;string, List&lt;Server&gt;&gt;
    _serverDictionary.Clear();

    using (TextReader textReader = File.OpenText(_csvFile.FullName))
    using (CsvReader reader = new CsvReader(textReader))
    {    
        while (reader.Read())
        {
            string applicationName = reader.GetField&lt;string&gt;("Application");
            Server server = reader.GetRecord&lt;Server&gt;();

            if (_serverDictionary.ContainsKey(applicationName))
                _serverDictionary[applicationName].Add(server);
            else
                _serverDictionary.Add(applicationName, new List&lt;Server&gt; { server });
        }
    }
}


Fetching:

public IEnumerable&lt;Server&gt; GetByApplication(string applicationName)
{
    List&lt;Server&gt; servers = new List&lt;Server&gt;();

    _appServers.Where(pair =&gt; pair.Key.EqualsIgnoreCase(applicationName))
        .ForEach(pair =&gt; servers.AddRange(pair.Value));

    return servers;
}


Concerns:


Is a Dictionary the best data structure to use in the first place?
I feel that both the mapping code and the fetch code could be more efficient by better utilizing LINQ projection.
I feel that both the mapping code and the fetch code could be more efficient by better utilizing LINQ projection.


more efficient yes but this time without linq.




if (serverDictionary.ContainsKey(applicationName))
    serverDictionary[applicationName].Add(server);
else
    serverDictionary.Add(applicationName, new List&lt;Server&gt; { server });



This is a very inconvenient way of adding new items to your dictionary because you are accessing it twice for each new item. With TryGetValue you can do the same with only one access to it. This means that if you could retrieve the value then you already have and it's ready to use, there's no need to use the indexer anymore. If however it's not there, then you just add it.

if (serverDictionary.TryGetValue(applicationName, out List&lt;Server&gt; servers))
{
    servers.Add(server);
}
else 
{
    serverDictionary[applicationName] = new List&lt;Server&gt; { server };
}





_appServers.Where(pair =&gt; pair.Key.EqualsIgnoreCase(applicationName))
    .ForEach(pair =&gt; servers.AddRange(pair.Value));



This might be super inefficient. If you use a dictionary then don't use linq if you don't have to. For fetching you can use the TryGetValue method agian:

return
    serverDictionary.TryGetValue(applicationName, out List&lt;Server&gt; servers) 
        ? servers
        : Enumerable.Empty&lt;Server&gt;();


Retrieving an item with TryGetValue means it's an O(1) operation whereas the same done with the original query means that the entire dictionary needs to be searched which means O(n). If you need to return a copy of the server list the you might use linq for it and call .ToList() on the servers.

There is one more thing about the linq query. You search for the key by ignoring the case. If you have multiple names but with different case then the resulting list will contain items of all those keys. Is this really what you want? The Where won't stop at the first match.

I don't know how you create the dictionary but if you want to ignore the case of the key you should initialize the dictionary appropriately by specifying the key comparer:

serverDictionary = new Dictionary&lt;string, List&lt;Server&gt;&gt;(StringComparer.OrdinalIgnoreCase);


Now the keys is case-insensitive also during adding.