[英]Create ILookup<int,int> from IDictionary<int,IEnumerable<int>>
Is there any elegant way how to transform有没有什么优雅的方式来转换
IDictionary<int, IEnumerable<int>>
into ILookup<int,int>
? IDictionary<int, IEnumerable<int>>
到ILookup<int,int>
? As far as I know it should be identical, but I find lookups to be more clear.据我所知,它应该是相同的,但我发现查找更清晰。
Story behind this is more complex but I'm forced to select List of ids together with its Lists of related ids:这背后的故事更复杂,但我不得不选择 ID 列表及其相关 ID 列表:
masters
.Select(m => new {masterId = m.Id, childIds = m.Children.Select(c => c.Id)})
.ToDictionary(k => masterId, v => v.childIds)
I would be happy to select directly Lookup but I don't know if its possible.我很乐意直接选择 Lookup,但我不知道是否可能。
example of master variable type can be simple as this:主变量类型的示例可以很简单,如下所示:
public class Master
{
public int Id { get; set; }
public List<Master> Children { get; set; }
}
As Lasse V. Karlsen suggested in the comments, you could create a wrapper type that exposes an ILookup
:正如 Lasse V. Karlsen 在评论中建议的那样,您可以创建一个公开
ILookup
的包装器类型:
public class LookupDictionary<TKey, TElement> : ILookup<TKey, TElement>
{
private readonly IDictionary<TKey, IEnumerable<TElement>> _dic;
public LookupDictionary(IDictionary<TKey, IEnumerable<TElement>> dic)
{
_dic = dic;
}
public int Count
{
get { return _dic.Values.Sum(x => x.Count()); }
}
public IEnumerable<TElement> this[TKey key]
{
get { return _dic.ContainsKey(key) ? _dic[key] : Enumerable.Empty<TElement>(); }
}
public bool Contains(TKey key)
{
return _dic.ContainsKey(key);
}
public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator()
{
return _dic.Select(kv => new LookupDictionaryGrouping(kv)).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
class LookupDictionaryGrouping : IGrouping<TKey, TElement>
{
private KeyValuePair<TKey, IEnumerable<TElement>> _kvp;
public TKey Key
{
get { return _kvp.Key; }
}
public IEnumerator<TElement> GetEnumerator()
{
return _kvp.Value.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public LookupDictionaryGrouping(KeyValuePair<TKey, IEnumerable<TElement>> kvp)
{
_kvp = kvp;
}
}
}
Well, you could flatten the dictionary and then convert it to a Lookup
:好吧,您可以将字典展平,然后将其转换为
Lookup
:
dict.SelectMany(kvp -> kvp.Value, (kvp, v) => new {k = kvp.Key, v})
.ToLookup(kvp => kvp.k, kvp => kvp.v)
but it's practically the same thing as a dictionary so it seems unnecessary.但它实际上与字典相同,因此似乎没有必要。
If I understand you correctly you want to flatten your collection.如果我理解正确,您想将您的收藏展平。 You can do it like this:
你可以这样做:
masters.SelectMany(x => x.Children, (x, y)
=> new {
ParentId = x.Id,
ChildId = y.Id
})
.ToLookup(x => x.ParentId, y => y.ChildId);
So you'll get your ILookup<int,int>
.所以你会得到你的
ILookup<int,int>
。 Moreover, you don't need any Dictionary
collection.此外,您不需要任何
Dictionary
集合。 But it' pretty much safe with Dictionary
.但是使用
Dictionary
非常安全。
You could do this - a bit more readable than pure lambdas... :)你可以这样做 - 比纯 lambdas 更具可读性... :)
Dictionary<int, IEnumerable<int>> dict = new Dictionary<int, IEnumerable<int>>();
dict.Add(1, new int[] {1, 2, 3});
dict.Add(2, new int[] {4, 5, 6});
dict.Add(3, new int[] {4, 5, 6});
var lookup = (from kv in dict
from v in kv.Value
select new KeyValuePair<int, int>(kv.Key, v)).ToLookup(k=>k.Key, v=>v.Value);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.