如何獲得Linq在C#中的返回SortedList
給定IEnumerable
?如果我不能,是否有可能將IEnumerable
轉換爲SortedList
?C#Linq return SortedList
10
A
回答
15
最簡單的方法可能是使用ToDictionary
創建字典,然後調用SortedList<TKey, TValue>(dictionary)
構造函數。另外,添加自己的擴展方法:
public static SortedList<TKey, TValue> ToSortedList<TSource, TKey, TValue>
(this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
Func<TSource, TValue> valueSelector)
{
// Argument checks elided
SortedList<TKey, TValue> ret = new SortedList<TKey, TValue>();
foreach (var item in source)
{
// Will throw if the key already exists
ret.Add(keySelector(item), valueSelector(item));
}
return ret;
}
這將允許您創建SortedList
s的匿名類型的值:
var list = people.ToSortedList(p => p.Name,
p => new { p.Name, p.Age });
4
您將需要使用IDictionary
構造函數,因此使用ToDictionary
擴展方法在你的linq查詢上,然後用新的SortedList(dictionary);
eg
var list=new SortedList(query.ToDictionary(q=>q.KeyField,q=>q));
0
事情是這樣的作品就好
List<MyEntity> list = DataSource.GetList<MyEntity>(); // whatever data you need to get
SortedList<string, string> retList = new SortedList<string, string>();
list.ForEach (item => retList.Add (item.IdField, item.Description));
相關問題
- 1. C#Linq SortedList篩選到SortedList
- 2. 來自SortedList的LINQ C#
- 3. SortedList和Linq
- 4. ASP.NET與W#C#SortedList
- 5. Linq groupby return unique group
- 6. 用Linq查詢SortedList <Object1,Object2>
- 7. 如何將SortedList轉換爲SortedList <>
- 8. C#SortedList,通過鍵獲取值
- 9. 嵌套SortedList
- 10. C++ Return Auto Array
- 11. QT Creator,return(C++)
- 12. WebMethod c#return double []
- 13. return * this C++
- 14. C#LINQ Z分數查詢輸出到字典<string,SortedList <DateTime,double >>
- 15. C# - 在SortedList中自動生成密鑰
- 16. C#SortedList如何在內部工作?
- 17. SortedList DataGridView刷新?
- 18. Changeed Key for SortedList
- 19. return not breaking loop(c#)
- 20. Linq查詢與SortedList <int,列表<int>>
- 21. 在SortedList上啓用foreach
- 22. Linq to Entities left join return 0 records
- 23. SortedList與SortedDictionary與Sort()
- 24. SortedList移植到Silverlight
- 25. SortedList似乎沒有排序
- 26. Android appcompat`SortedList`的度假村?
- 27. Objective C:-1 <0 return false
- 28. Win32 C++ Return cout功能
- 29. 沒有值的C++「return」
- 30. c + +沒有return語句