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: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());
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());
Source: stackoverflow.com
Related Query
- 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
More Query from same tag
- Joining of two queries and returning in list format
- Is there an opposite of LINQ's All method?
- Return one result from LINQ Query
- VS 2013 telling me Use lamba expression
- Linq - Count <li> tags in a block of HTML
- How to add list of missing values in c# list using Linq
- How to search with LINQ entites having string[] array properties and find these containing any of string from string[] array?
- How to reference a parent LINQ query from within a subquery
- Finding the distinct values of a dictionary
- group list by first four letters of a field and set the value
- How to filter nested entities of the same type?
- How to select only the records with the highest date in LINQ
- What's wrong with this EF Join method call?
- Can I combine a foreach and a LINQ query into one?
- Pass ViewBag from Controller to Partial view
- Asp.Net MVC3, Update query in Linq
- Assign anonymous in IF-ELSE statements
- Best way get first word and rest of the words in a string in C#
- LINQ: Cannot get more than two Where clauses to work
- LINQ-To-Sql Update - Performance Issues
- Convert List of Different Objects from DB to List of object in Entity Framework Core
- NHibernate Group By Many-to-Many Association
- LINQ Error with GroupBy
- Use .Select().Join() to Join Two DataTables
- Efficient way of finding multiple dates per ID
- Linq to XML get specific value from element
- Concat lists from dictionary-values to IEnumerable
- LINQ InheritanceMappingAttribute Code property
- Refactoring method that binds controls to LINQ
- Linq Scope Problem + Reduce Repeated Code