1

我正在处理 C# 2.0/.NET 2.0 中的一个问题,其中我有一个 Sortedlist,并且想要搜索此 SortedList 的所有“值”(而不是“键”)以查找某个子字符串并计算出现次数.

这就是我想要做的:

{
   Sortedlist<string,string> mySortedList;
   // some code that instantiates mySortedList and populates it with data
   List<string> myValues = mySortedList.Values;  // <== does not work
   int namesFound = myValues.FindAll(ByName(someName)).Count;
}

当然,这不起作用,因为 mySortedList.Values 返回一个 IList,而“myValues”是一个列表。我尝试“强制转换”IList 以便 myValues 接受它,但它似乎不起作用。

当然,我可以在“foreach”循环中循环 mySortedList.Values,但我真的不想这样做。

有人有什么建议吗?

EDIT-1:好的,看起来没有本地方法可以轻松做到这一点。我以为我只是错过了一些东西,但显然我没有。所以我想我只是要对 IList 做一个“foreach”。

感谢大家的反馈!我给每个人投了 1 票,因为我认为所有的反馈都很好。再次感谢!:-)

EDIT-2:看起来 CMS 有我正在寻找的答案。唯一需要注意的是(正如 Qwertie 指出的那样),这可能会导致性能下降,因为它涉及将所有值复制到另一个列表,然后从头到尾搜索该列表。所以对于短名单,这个答案是有效的。更长的列表?好吧,这由你决定......

4

5 回答 5

2

您不能将 Values 属性强制转换为 List<string>,因为它不是 List<string>——它是 Dictionary<TKey, TValue>.ValueCollection。

但是如果你使用LinqBridge(对于 .NET Framework 2.0 和 C# 3.0),这个问题很容易用 LINQ 解决,如下所示:

SortedList<string, string> m = ...;
int namesFound = m.Values.Where(v => v.Contains("substring")).Count();

(if you're still on C# 2.0, you can use Poor-man's LINQ instead, with slightly more work)

于 2009-03-06T01:08:36.330 回答
2

Since the IList Interface implements IEnumerable, you can actually get a List<T> of values using the List<T> (IEnumerable) Constructor:

List<string> myValues = new List<string>(mySortedList.Values);
于 2009-03-06T01:35:51.723 回答
1

太糟糕了,你在 .Net 2.0 中。这正是 LINQ 的用途。;)

你不能真正做到这一点,因为 FindAll() 是 List 的成员。您可以在 mySortedList.Values 上创建一个新列表,但这似乎是一种浪费,因为需要分配一个新对象和底层数组来调用一个函数。

我只是在某个类中为名为 FindAll() 的列表编写一个实用函数,然后传递您的 IList 和委托。

于 2009-03-06T01:03:10.757 回答
1
    static void Main(string[] args)
    {
        string someName = "two";
        SortedList<string, string> mySortedList = new SortedList<string,string>()
        {
            {"key1", "This is key one"},
            {"key2", "This is key two"},
            {"key3", "This is key three"},
        };

        int namesFound = mySortedList.Values.Where(i => i.Contains(someName)).Count();
        Console.WriteLine(namesFound);
        Console.ReadKey();
    }

在 Framework 2.0 中,也许可以这样做:

    static void Main(string[] args)
    {
        string someName = "two";
        SortedList<string, string> mySortedList = new SortedList<string,string>()
        {
            {"key1", "This is key one"},
            {"key2", "This is key two"},
            {"key3", "This is key three"},
        };

        int namesFound = FindAll(mySortedList.Values, someName).Count ;
        Console.WriteLine(namesFound);
        Console.ReadKey();
    }
    public static IList<String> FindAll(IList<String> items, string item)
    {
        List<String> result = new List<string>();
        foreach (String s in items)
        {
            if (s.Contains(item))
            {
                result.Add(s);
            }
        }
        return result;
    }

但那是你真正不想做的。

于 2009-03-06T01:06:19.830 回答
1

你试过了SortedListGetValueList?这会给你一个IList价值。

于 2009-03-06T01:08:29.120 回答