I have an object that looks like this:
class Model
{
public string Category {get;set;}
public string Description {get;set;}
}
Currently I am getting the whole list of those objects with Linq, and then manually setting up a dictionary, like this:
List<Model> models = //get list from repository and then with linq order them by category and description
Dictionary<string, List<Model>> dict= new Dictionary<string, List<Model>>();
foreach (var m in models) {
if (dict.ContainsKey(m.Category))
{
dict[m.Category].Add(m);
}
else
{
dict.Add(m.Category, new List<Model> { m });
}
}
this way I can access all of the models of a certain category by using the keys.
Is there a way to generate the dictionary straight with a LINQ query?
Thanks!