score:3
Given an IEnumerable<ContainerClass> named objects
, you could do this:
var dict = objects
.ToLookup(o => o.Group)
.ToDictionary(grp => grp.Key, grp => grp.Select(o => o.Value).ToList());
Explanation:
ToLookup
method converts the IEnumerable<ContainerClass>
to an ILookup<Guid, ContainerClass>
. The ToDictionary
method then takes the collection of IGrouping<Guid, ContainerClass>
objects, and selects the Key
of the grouping as the key for the dictionary, and then selects the Value
property of the ContainerClass
objects, and converts that to a List<int>
for the dictionary values.
You may not actually need a Dictionary<ContainerClass>
. If you're OK with using an ILookup<Guid, int>
instead, you can just do this:
var lookup = objects.ToLookup(o => o.Group, o => o.Value);
score:7
Rather than a Dictionary<Guid, List<int>>
could you use a Lookup<Guid, int>
? That would make it easy:
var lookup = list.ToLookup(x => x.Group, x => x.Value);
A Lookup
is much like a Dictionary
, but designed to work with multiple values per key. On interesting difference is that if you fetch a missing key, you'll just get an empty sequence back instead of an exception.
If you can use that, that's great. Otherwise you could always convert from a Lookup
to a Dictionary
:
var dictionary = list.ToLookup(x => x.Group, x => x.Value)
.ToDictionary(x => x.Key, x => x.Value.ToList());
score:4
I'm assuming you have multiple instances of the class, otherwise it is trivial. In LINQ, something similar is:
var lookup = data.ToLookup(obj => obj.Group, obj => obj.Value);
Then you can, for example:
foreach(var value in lookup[someGuid]) {
...
}
If you really need this as a dictionary (rather than a lookup, which is close):
var lookup = data.GroupBy(obj => obj.Group).ToDictionary(
grp => grp.Key, grp => grp.Select(obj => obj.Value).ToList());
Source: stackoverflow.com
Related Articles
- LINQ - Convert List to Dictionary with Value as List
- Linq: Convert list of object to dictionary, with an object member as value
- Return list with more than one value stored in a dictionary using linq
- How to convert dictionary with object to list of that type?
- C# Using LINQ to filter each Object with Max Value in List of Objects
- LINQ - Grouping a list by multiple properties and returning an object with an array member
- Find object in list with given property value then find dictionary value
- How to get list of keys in a dictionary with group by value with using LinQ
- Combine contents of list member of object collection into single list with linq
- C# Linq adding to dictionary with list as value
- how to get the value member of a selected comboBox to use it to add a new object with linq
- Create a tree structure in linq with a single list source with parent - child as strings of an object
- linq lambda convert object with list of objects of the same type to another object
- Use LINQ to convert List of Dictionary to strongly Typed Class object List
- Dictionary lookup with LinQ to find a subset where the Value is a List element
- How to use LINQ to select object with minimum or maximum property value
- Convert list to dictionary using linq and not worrying about duplicates
- Create a list from two object lists with linq
- Convert an array to dictionary with value as index of the item and key as the item itself
- Convert dictionary values to list using linq
- Convert CollectionBase to List or data type usable with Linq
- Linq Query Dictionary where value in List
- C# LINQ - convert nested dictionary to a list
- LINQ Where clause with Contains where the list has complex object
- Convert a list to a dictionary and sum up values using linq
- List to Dictionary with incremental keys in one line LINQ statement
- Convert string array to custom object list using linq
- Count number of given object in a list with LINQ
- Linq to entities - SQL Query - Where list contains object with 2 properties (or more)
- LINQ error: The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type
- Eleminate duplicates Items from a List<T> by foreach loop in C#
- Read and Remove invalid characters from xml outside xml elements in C# Linq to Xml
- "System.InvalidCastException" error when retrieving data from SQL Server 2012 Express
- XML delete node according to timestamp C#
- How to do LEFT JOIN in LINQ to Entities?
- C# Razor Linq, one generic variable giving unexpected results
- how to access output value of Special Select SQL Query in the C# program
- how to get specific collection from a collection with linq?
- Entity Framework LINQ
- converting query to list (not working) asp.net mvc
- LINQ to Entities does not recognize the method 'System.DateTime GetDate()' method, and this method cannot be translated into a store expression
- How can I get the last 50 elements in a list?
- c# linq to xml to list
- Retrieving data from Oracle db in asp.net mvc using linq
- Using Dynamic Linq to Filter entity by datetimeoffset columns
- How to use an aggregate function in linq
- Extract rows from 2D array within a model
- Unable to convert string to int in LINQ query from MongoDB
- C# List grouping and assigning a value
- Multiple Joins LINQ Query performance