2011-01-11 46 views
0

我有一个包含线程信息Dictionary<String,Thread>从列表中找到索引

"2FF" 
"2IE" 
"7CH" 

等字典

我知道什么是整数2,7等什么,我想知道的是,在字典多少字符串包含给定的整数,如果它的存在,然后拿到串

String GetString(int integer) 
{ 
//if Dictionary contains given intgr return whole string in which that integer is present 
} 

}

+2

哪里是多线程方面在这里?这是一个列表还是字典? – 2011-01-11 19:15:48

+2

这个问题不是很清楚... – gsharp 2011-01-11 19:18:50

+0

这个问题很混乱。 – 2011-01-11 19:18:51

回答

3

使用LINQ语法:

var matchingThreads = from pair in dictionary 
        where pair.Key.StartsWith(number.ToString()) 
        select pair.Value; 

与传统的语法:

var matchingThreads = dictionary 
       .Where(pair => pair.Key.StartsWith(number.ToString())) 
       .Select(pair => pair.Value); 

如果你只需要计数他们,你不关心Thread对象,可以用途:

int count = dictionary.Keys.Count(key => key.StartsWith(number.ToString())) 

N注意你需要一个using System.Linq指令。

0

也许列表<CustomClass>会是一个更好的选择,在这些地方CustomClass会是什么样子:

public sealed class CustomClass 
{ 
    public Thread Thread { get; set; } 
    public string String { get; set; } 
} 

(更好的属性名称时总是好的,当然:-))的

字典是如果你不知道确切的关键字或者只是其中的一部分,那么就不是可以编辑的。

然后,您可以使用LINQ找出你想要的东西,例如:

int count = list.Where(c => c.String.StartsWith(integer.ToString())).Count(); 
//or 
IEnumerable<string> strings = list.Where(c => c.String.StartsWith(integer.ToString())).Select(c => c.String); 
0
public IEnumerable<string> GetMatchingKeys(int value) 
{ 
    var valueText = value.ToString(); 

    return _dictionary.Keys.Where(key => key.Contains(valueText)); 
}