LINQ Join on Dictionary<K,T> where only K is changed.
- by Stacey
Assuming type TModel, TKey, and TValue.
In a dictionary where KeyValuePair is declared, I need to merge TKey into a separate model of KeyValuePair where TKey in the original dictionary refers to an identifier in a list of TModel that will replace the item in the Dictionary.
public TModel
{
public Guid Id { get; set; }
// ...
}
public Dictionary<Guid, TValue> contains the elements. TValue relates to the TModel.
The serialized/stored object is like this..
public SerializedModel
{
public Dictionary<Guid,TValue> Items { get; set; }
}
So I need to construct a new model...
KeyValueModel
{
public Dictionary<TModel, TValue> { get; set; }
}
KeyValueModel kvm = = (from tModels in controller.ModelRepository.List<Models.Models>()
join matchingModels in storedInformation.Items on tModels.Id equals matchingModels
select tModels).ToDictionary( c => c.Id, storedInformation.Items.Values )
This linq query isn't doing what I'm wanting, but I think I'm at least headed in the right direction. Can anyone assist with the query?
The original object is stored as a KeyValuePair. I need to merge the Guid Keys in the Dictionary to their actual related objects in another object (List) so that the final result is KeyValuePair.
And as for what the query is not doing for me... it isn't compiling or running. It just says that "Join is not valid".