I have List<Dictionary<DateTime, Points[]>> taskResult generated from tasks
var taskResult = tasks.Select(t => t.Result).ToList();
var data = new Dictionary<DateTime, Points[]>();
in my function I want to return Dictionary<DateTime, Points[]> data but I cant figure out how to do that. I tried using foreach but had no luck
Enumerable.SelectMany extension method is right tool for the job, which combines many collections into one. Dictionary is a collection of key-value pairs.
var combined = dictionaries
.SelectMany(dictionary => dictionary.Select(pair => pair))
.GroupBy(pair => pair.Key)
.ToDictionary(
group => group.Key,
group => group.SelectMany(pair => pair.Value).ToArray());
Approach above will merge points of same date if original dictionaries contain duplicated dates
Because Dictionary implements IEnumerable you can remove .Select in first call of SelectMany.
Alternative for .GroupBy is .ToLookup method, which can have multiple values per one key.
var combined = dictionaries
.SelectMany(dictionary => dictionary)
.ToLookup(pair => pair.Key, pair.Value)
.ToDictionary(
lookup => lookup.Key,
lookup => lookup.SelectMany(points => points).ToArray());
Related
I want to merge the values of multiple dictionaries (3 to be exact) into a list. My current solution uses linq to first combine the dictionaries and then converts the values into a list.
private List<Part> AllParts()
{
return walls.Concat(floors)
.Concat(columns)
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value)
.Values
.ToList();
}
Merging the lists first seems redundant. How can I improve this?
You can simplify this code by concatenating your dictionaries and selecting values without converting to a dictionary:
return walls.Concat(floors)
.Concat(columns)
.Select(kvp => kvp.Value)
.ToList();
It looks like the shortest and most readable solution. You can avoid concatenating your collections by taking values only:
return walls.Values
.Concat(floors.Values)
.Concat(columns.Values)
.ToList();
However, I do not see any readability, maintainability or performance improvements here.
P.S. I assumed that there are no duplicates in your dictionaries. This code will contain duplicated values while ToDictionary approach will throw exceptions on key duplication.
First off, some useful links:
Combine multiple dictionaries into a single dictionary
Combine multiple dictionaries with same key in them into one dictionary with the sum of values
Merging dictionaries in C#
Basically, merging the dictionaries first is a must, but there are more efficient ways than yours to avoid duplicates, such as:
Option 1
var dictionaries = new[] { walls, floors, columns };
var result = dictionaries
.SelectMany(d => d)
.GroupBy(
kvp => kvp.Key,
(key, kvps) => new { Key = key, Value = kvps.Sum(kvp => kvp.Value) }
)
.ToDictionary(x => x.Key, x => x.Value).ToList();
return result;
Works with any number of dictionaries, not just 3.
Option 2
var result = walls.Union(floors).Union(columns)
.ToDictionary (k => k.Key, v => v.Value).ToList();
return result;
to avoid duplicates:
var result = walls.Concat(floors).Concat(columns).GroupBy(d => d.Key)
.ToDictionary (d => d.Key, d => d.First().Value).ToList();
Dictionary exposes a property called Values, which is basically a read-only collection of all the values in that Dictionary. This is the simplest way to get all the value when you don't care about the key, and it's the easiest way to combine them:
var allParts = walls.Values
.Concat(floors.Values)
.Concat(columns.Values);
This is much simpler, and possibly more performant, than various methods of merging dictionaries, using LINQ queries to convert KeyValuePairs to Parts and so on - you don't care about the Dictionariness here, only the list of values - so treat it as a list of values.
One thing, though, is that this will not strip out duplicate Parts, if any exist. You can do that by using Union() instead of Concat().
I often find myself doing the below to extract properties from a list of objects just to create an aggregated list. How would this be expressed with LINQ?
var totalErrors =
new Dictionary<string, string>();
foreach (var res in results)
{
foreach (var err in res.Errors)
{
totalErrors
.Add(err.Key, err.Value);
}
}
return totalErrors;
You can use SelectMany and ToDictionary methods:
var result = results
.SelectMany(x => x.Errors) // get all Errors in one sequence
.ToDictionary(x => x.Key, x => x.Value); // create new dictionary based on this Enumerable
SelectMany() projects each element of a sequence to an IEnumerable<T> and flattens the resulting sequences into one sequence. And ToDictionary() creates a Dictionary<TKey, TValue> from an IEnumerable<T> according to a specified key selector function.
You can do an aggregation on two levels with SelectMany, like this:
var totalErrors = results
.SelectMany(r => r.Errors)
.ToDictionary(e => e.Key, e => e.Value);
SelectMany "flattens" a collection of collections into a single level, at which point you can apply ToDictionary to a flattened list.
You can use SelectMany :
var allErrors = Results.SelectMany( res=>res.Errors );
//foreach( var error in allErrors )...
var dictionary = allErrors.ToDictionary( x=>x.Key, x=> x.Value );
So I have A dictionary (Employees2Name) Of int => (some class) which I need to turn into a sorted list of key value pairs of int => (some property in the class)
I have this working fine which is the good news. It just seems like I'm doing an extra step is there a way to shorten this in linq with a cast.
ComboBoxValues.Employees2Name.Select(k => new {Key = k.Key, Value = k.Value.Name})
.ToDictionary(k => k.Key, v => v.Value)
.ToList<KeyValuePair<int, string>>()
.OrderBy(kp => kp.Value)
The second to dictionary seems redundant.
It seems that all you need is
ComboBoxValues.Employees2Name
.Select(k => new KeyValuePair<int, string>(k.Key, k.Value.Name))
.OrderBy(item => item.Value);
Just Select and OrderBy; try no to materialize (i.e. ToList(), ToDictionary()) especially in the middle of the Linq.
#Servy Comments reflects the best answer.
Your already have this in an
IEnumberable<KeyPairValue<int, Class>> you just need to put the name to a dictionary then order by
#Html.PopulateCombobox(ComboBoxValues.Employees2Name
.ToDictionary(k => k, v => v.Value.Name)
.OrderBy(v => v.Value)
Dictionary class already implements IEnumerable>, that is a valid input for your OrderBy() then applying a ToList>() seems totally useless.
More, I think that the ToDictionary call is a waste of memory and time, because you are constructing the dictionary (which main purpose is to keep items unique by key) from a plain collection of items and later sort them by value (rather than key), thus without taking any advantage from the Dictionary<,> class.
I would rewrite your code as
ComboBoxValues.Employees2Name.Select(k => new KeyValuePair<int, string>(k.Key, k.Value.Name))
.OrderBy(kp => kp.Value)
Regards,
Daniele.
No need to use select and orderby. You can just try this
SortedList<int, string> sortedList =
new SortedList<int, string>(ComboBoxValues.Employees2Name
.ToDictionary(i => i.Key, i => i.Value.Name));
I have a Dictionary<string, string> and another List<string>. What I am trying to achieve is a linq query to get all items out of the dictionary where any values from said dictionary are in the List<string>.
I found this post to be helpful, LINQ querying a Dictionary against a List . And was able to write the following linq expression, however my results never actually return anything.
What I have so far.
Data is the dictionary and PersonList is the list of strings.
var Persons = PersonList.Where(x => Data.ContainsKey(x))
.Select(z => new { key = z, value = Data[z] })
.ToList();
Are you looking for keys or values? If you're looking for values use
var Persons = Data.Where(kvp => PersonList.Contains(kvp.Value))
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
If instead you really want keys then your code should work but another option would be:
var Persons = Data.Where(kvp => PersonList.Contains(kvp.Key))
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
Try this one:
var Persons = Data.Where(x=>PersonList.Contains(x.Value))
.Select(x=>new { key=x.Key, value=x.Value})
.ToList();
I converted the result to a list, because I noticed that you used it in your code. If you want it to a dictionary, just take a look to the answer provided by D Stanley.
I think you don't have to convert it ToDictionary, because your source is a dictionary:
var Persons = Data.Where(kvp => personList.Contains(kvp.Key))
.Select(x => x);
I quickly tested it in LinqPad, but if this is a bad idea or I'm wrong, please leave a comment.
I would like to take a list of objects and convert it to a dictionary where the key is a field in the object, and the value is a list of a different field in the objects that match on the key. I can do this now with a loop but I feel this should be able to be accomplished with linq and not having to write the loop. I was thinking a combination of GroupBy and ToDictionary but have been unsuccessful so far.
Here's how I'm doing it right now:
var samplesWithSpecificResult = new Dictionary<string, List<int>>();
foreach(var sample in sampleList)
{
List<int> sampleIDs = null;
if (samplesWithSpecificResult.TryGetValue(sample.ResultString, out sampleIDs))
{
sampleIDs.Add(sample.ID);
continue;
}
sampleIDs = new List<int>();
sampleIDs.Add(sample.ID);
samplesWithSpecificResult.Add(sample.ResultString, sampleIDs);
}
The farthest I can get with .GroupBy().ToDictionay() is Dictionary<sample.ResultString, List<sample>>.
Any help would be appreciated.
Try the following
var dictionary = sampleList
.GroupBy(x => x.ResultString, x => x.ID)
.ToDictionary(x => x.Key, x => x.ToList());
The GroupBy clause will group every Sample instance in the list by its ResultString member, but it will keep only the Id part of each sample. This means every element will be an IGrouping<string, int>.
The ToDictionary portion uses the Key of the IGrouping<string, int> as the dictionary Key. IGrouping<string, int> implements IEnumerable<int> and hence we can convert that collection of samples' Id to a List<int> with a call to ToList, which becomes the Value of the dictionary for that given Key.
Yeah, super simple. The key is that when you do a GroupBy on IEnumerable<T>, each "group" is an object that implements IEnumerable<T> as well (that's why I can say g.Select below, and I'm projecting the elements of the original sequence with a common key):
var dictionary =
sampleList.GroupBy(x => x.ResultString)
.ToDictionary(
g => g.Key,
g => g.Select(x => x.ID).ToList()
);
See, the result of sampleList.GroupBy(x => x.ResultString) is an IEnumerable<IGrouping<string, Sample>> and IGrouping<T, U> implements IEnumerable<U> so that every group is a sequence of Sample with the common key!
Dictionary<string, List<int>> resultDictionary =
(
from sample in sampleList
group sample.ID by sample.ResultString
).ToDictionary(g => g.Key, g => g.ToList());
You might want to consider using a Lookup instead of the Dictionary of Lists
ILookup<string, int> idLookup = sampleList.ToLookup(
sample => sample.ResultString,
sample => sample.ID
);
used thusly
foreach(IGrouping<string, int> group in idLookup)
{
string resultString = group.Key;
List<int> ids = group.ToList();
//do something with them.
}
//and
List<int> ids = idLookup[resultString].ToList();
var samplesWithSpecificResult =
sampleList.GroupBy(s => s.ResultString)
.ToDictionary(g => g.Key, g => g.Select(s => s.ID).ToList());
What we 're doing here is group the samples based on their ResultString -- this puts them into an IGrouping<string, Sample>. Then we project the collection of IGroupings to a dictionary, using the Key of each as the dictionary key and enumerating over each grouping (IGrouping<string, Sample> is also an IEnumerable<Sample>) to select the ID of each sample to make a list for the dictionary value.