来自 SortedList C# 的 LINQ

发布于 2024-08-23 18:36:24 字数 304 浏览 6 评论 0原文

我对 C# 相当陌生,对 LINQ 也完全陌生。

我有一个 SortedList 类型的现有对象,我想使用 LINQ 对其进行过滤。然而,我似乎对此有疑问,显然是因为 SortedList 类型不是通用的。

目前,我已经循环遍历了整个 SortedList,并将其中的对象手动添加到列表中,并使用 LINQ 从那里进行过滤。然而,在我看来,这似乎是垃圾。

有没有更好的方法将我的 SortedList 转换为 List<> (我不关心保存密钥)?或者确实将我的 SortedList 转换为 SortedList<>也许?

I'm fairly new to C#, and completely new to LINQ.

I have an existing object of type SortedList, which I would like to filter through using LINQ. However I appear to be having issues with this, apparently as the SortedList type is not generic.

For the timebeing I've looped through the entire SortedList and manually added the objects within to a List and used LINQ to filter from there. However, that seems to me, well, rubbish.

Is there a better way to convert my SortedList to List<> (I'm not bothered about preserving the key)? Or indeed to convert my SortedList to SortedList<> perhaps?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

魂归处 2024-08-30 18:36:24

原始的SortedList类是Collection的一部分。泛型引入了 SortedList,但如果您有旧代码,那么它就不会使用它。

最简单的方法是使用可用于 SortedList 的 Cast 方法或 OfType 扩展方法。这些方法将为您提供 IEnumerable 列表,您可以应用通用 Linq 操作。确保在开头包含 using System.Linq; 然后您应该能够执行如下操作:

sortedList.Cast<int>().Where(i=>i > 5);
sortedList.OfType<Car>().Where(c=> c.Color == Color.Blue);

Original SortedList class is part of Collection. Generics introduces SortedList but if you have old code, then it wont be using that.

The easiest is to use the Cast method or OfType extension method that is made available for SortedList. Those methods will give you IEnumerable list that you can apply generic Linq operations. Make sure you include using System.Linq; in the beginning then you should be able to do like below:

sortedList.Cast<int>().Where(i=>i > 5);
sortedList.OfType<Car>().Where(c=> c.Color == Color.Blue);
终止放荡 2024-08-30 18:36:24

如果不需要按键过滤,只需按值进行转换和过滤

SortedList sl = new SortedList();
sl.Add("foo", "baa");
var baas = sl.Values.Cast<string>().Where(s => s == "baa");

If you don't need to filter by Keys, just cast and filter on Values

SortedList sl = new SortedList();
sl.Add("foo", "baa");
var baas = sl.Values.Cast<string>().Where(s => s == "baa");
吹泡泡o 2024-08-30 18:36:24

怎么样

normalList = sortedList.Items;

这会给你你想要的清单吗?

What about

normalList = sortedList.Items;

Does that give you the list you want?

反目相谮 2024-08-30 18:36:24

要回答“是否有更好的方法将我的 SortedList 转换为 List<> (我不关心保留密钥)?”...这是示例,最后一行作为答案,导致列表为整数 4、1、3、2:

SortedList<string,int> sortedList = new SortedList<string,int>();
sortedList.Add("One", 1);
sortedList.Add("Two", 2);
sortedList.Add("Three", 3);
sortedList.Add("Four", 4);
List<int> list = sortedList.Values.ToList();

To answer the question "Is there a better way to convert my SortedList to List<> (I'm not bothered about preserving the key)?"... Here is example, with last line as the answer, resulting in list with integers 4, 1, 3, 2:

SortedList<string,int> sortedList = new SortedList<string,int>();
sortedList.Add("One", 1);
sortedList.Add("Two", 2);
sortedList.Add("Three", 3);
sortedList.Add("Four", 4);
List<int> list = sortedList.Values.ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文