简体   繁体   English

有没有办法压扁.Net ILookup <TKey, TElement> 进入清单 <TElement> ?

[英]Is there a way to flatten a .Net ILookup<TKey, TElement> into a List<TElement>?

Is there a quick way to get a flattened List<TElement> from an ILookup<TKey, TElement> that was created from the IEnumerable<TElement> extension? 有没有快速的方法从ILookup<TKey, TElement>中获取从IEnumerable<TElement>扩展创建的扁平List<TElement>


Updated with example 更新了示例

List<int> list = new List<int>();
var lookup = list.ToLookup(key => key);
list = lookup.?? // How to convert the lookup back to the list
lookup.SelectMany( x => x ).ToList()

然而,向ILookup转换回来很可能会改变顺序。

Not sure if this is what you are after. 不确定这是不是你想要的。 For Dictionary<> to List<> 对于Dictionary<> to List<>

List<TElement> list iLookUp.Values.ToList<TElement>();

from List<> to Dictionary<> List<>Dictionary<>

var dict = list.Cast<TElement>().ToDictionary(t => t.Id, t => t.Description);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不应该ILookup <TKey, TElement> 是(声明)TElement中的协变? - Shouldn't ILookup<TKey, TElement> be (declared) covariant in TElement? 枚举ILookup结果的成本? 最好总是使用Dictionary <TKey,List<TElement> &gt;? - Cost of enumerating ILookup results? Better to always use Dictionary<TKey,List<TElement>>? Lookup 的意义是什么<tkey, telement> ?</tkey,> - What is the point of Lookup<TKey, TElement>? 多个线程从Lookup读取是否安全 <TKey, TElement> ? - Is it safe for multiple threads to read from a Lookup<TKey, TElement>? 显示 ObservableGroupedCollection 的正确方法<string, telement>使用 Wpf .NET 6 和 CommunityToolkit.Mvvm 包</string,> - Proper way of displaying an ObservableGroupedCollection<string, TElement> using Wpf .NET 6 and the CommunityToolkit.Mvvm Package 在DB Context C#中使用动态类型代替TElement - Use dynamic type in place of TElement in DB Context C# C# 使用 generics 访问 static 变量(EmptyEnumerable<telement> 。实例)</telement> - C# accessing static variable with generics (EmptyEnumerable<TElement>.Instance) 使用LINQ将IEnumerable映射到ILookup的紧凑方法 - Compact way to map an IEnumerable to an ILookup with LINQ 身份证<TKey, TValue>在 .NET 4 中不是协变的 - IDictionary<TKey, TValue> in .NET 4 not covariant 转换字典<TKey, List<TValue> &gt; 到 ReadOnlyDictionary <TKey, ReadOnlyCollection<TValue> &gt; - Converting Dictionary<TKey, List<TValue>> to ReadOnlyDictionary<TKey, ReadOnlyCollection<TValue>>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM