C# 字典:搜索具有相似功能的键字符串

C# 字典:搜索具有相似功能的键字符串,c#,dictionary,C#,Dictionary,我想用相似的功能在字典中搜索我的关键字。我想取以“a”开头的钥匙,或者钥匙的第三个字母是“e”,或者钥匙的第四个字母不是“d” 在sql中,可以编写查询“where(类似于“a”的键)和(不类似于“d””我希望字典具有此功能。你有什么建议吗 谢谢 只需使用Linq: var query = myDict.Where(x => x.Key.IndexOf('a') > -1 && x.Key.IndexOf("d_") == -1); 您可以访问字典的Keys属性,然

我想用相似的功能在字典中搜索我的关键字。我想取以“a”开头的钥匙,或者钥匙的第三个字母是“e”,或者钥匙的第四个字母不是“d”

在sql中,可以编写查询“where(类似于“a”的键)和(不类似于“d””我希望字典具有此功能。你有什么建议吗

谢谢

只需使用Linq:

var query = myDict.Where(x => x.Key.IndexOf('a') > -1 && x.Key.IndexOf("d_") == -1);

您可以访问字典的Keys属性,然后使用Linq查询评估密钥:

var dictionary = new Dictionary<string,string>();

dictionary.Keys.Where( key => key.Contains("a")).ToList();
var dictionary=newdictionary();
dictionary.Keys.Where(key=>key.Contains(“a”)).ToList();
您可以使用LINQ

像这样的

myDic.Where(d=>d.Key.StartWith("a")).ToDictionary(d=>d.Key,d=>d.Value)


虽然这相当于SQL表扫描,但您可以使用LINQ或
IEnumerable
扩展方法在字典中搜索键与模式匹配的所有值:

扩展方法:

var values = dictionary.Where(pv => 
             pv.Key.StartsWith("A") || 
             (pv.Key.Length >= 3 && pv.Key[2] == 'e') || 
             pv.Key.Length < 4 || 
             pv.Key[3] != 'd').Select(pv => pv.Value);
var values=dictionary.Where(pv=>
pv.Key.StartsWith(“A”)||
(pv.Key.Length>=3和pv.Key[2]==e')||
pv.Key.Length<4||
pv.键[3]!='d')。选择(pv=>pv.值);
林克:

var值=(来自字典中的pv
其中pv.Key.StartsWith(“A”)||
(pv.Key.Legnth>=3&&pv.Key[2]==e')||
pv.长度<4||
pv.键[3]!='d'
选择pv.值);
请注意,这两个谓词的最后一部分都与“第四个字母不是“d”有关。我认为这意味着一个长度为三个字符(或更少)的字符串将与此匹配。如果您的意思是该字符串至少有四个字符,而其第四个字符不是“d”,那么变化应该是显而易见的


请注意,
Dictionary
类的主要(性能)好处是使用基于散列的键查找,它(在平均和最佳情况下)是O(1)。使用这样的线性搜索是O(n),因此,一般来说,类似这样的搜索会比普通键查找慢。

我想出了一个小小的扩展:

public static IList<string> KeysLikeAt(this Dictionary<string, object> dictionary, char letter, int index)
{
    return dictionary.Where(k => k.Key.Length > index && k.Key[index] == letter)
        .Select(k => k.Key).ToList();
}

public static IList<string> KeysNotLikeAt(this Dictionary<string, object> dictionary, char letter, int index)
{
    return dictionary.Where(k => k.Key.Length > index && k.Key[index] != letter)
        .Select(k => k.Key).ToList();
}
公共静态IList KeysLikeAt(此字典、字符、int索引)
{
返回dictionary.Where(k=>k.Key.Length>index&&k.Key[index]==字母)
.Select(k=>k.Key).ToList();
}
公共静态IList KeysNotLikeAt(此字典、字符、int索引)
{
返回dictionary.Where(k=>k.Key.Length>index&&k.Key[index]!=字母)
.Select(k=>k.Key).ToList();
}
您可以这样使用它:

IList<string> keysStartingWithA = dictionary.KeysLikeAt('a', 0);

IList<string> keysNotStartingWithD = dictionary.KeysNotLikeAt('d', 0);
IList keystartingwitha=dictionary.KeysLikeAt('a',0);
IList keysNotStartingWithD=dictionary.KeysNotLikeAt('d',0);

我对此投赞成票,但我认为重要的是要记住,我们实际上执行了线性扫描以应用条件。
Select(x=>x)
什么也不做。虽然在实际的LINQ表达式中需要一个
select
语句,但在只使用LINQ扩展方法的代码中不需要它,除非您实际正在执行转换。此外,这与他请求的逻辑不匹配,但我已经投票了,因为它指出了实际的概念。您为什么要这样做实际上使用
IndexOf
在字符串中进行搜索,而不仅仅是在
string
类中使用索引器(当然是在检查长度时)?
var values = (from pv in dictionary
              where pv.Key.StartsWith("A") ||
                    (pv.Key.Legnth >= 3 && pv.Key[2] == 'e') ||
                    pv.Length < 4 ||
                    pv.Key[3] != 'd'
                    select pv.Value);
public static IList<string> KeysLikeAt(this Dictionary<string, object> dictionary, char letter, int index)
{
    return dictionary.Where(k => k.Key.Length > index && k.Key[index] == letter)
        .Select(k => k.Key).ToList();
}

public static IList<string> KeysNotLikeAt(this Dictionary<string, object> dictionary, char letter, int index)
{
    return dictionary.Where(k => k.Key.Length > index && k.Key[index] != letter)
        .Select(k => k.Key).ToList();
}
IList<string> keysStartingWithA = dictionary.KeysLikeAt('a', 0);

IList<string> keysNotStartingWithD = dictionary.KeysNotLikeAt('d', 0);