简体   繁体   中英

c# dictionary key partial

I have a dictionary like that

 Key                  Value
 D:\test\ccc.csproj   1224
 D:\LOL\ppp.csproj    3467

Now when I'm searching for the key I have only part of the key which is the the project name (ccc or ppp) and not the exact key. Can you help me change code so it will work ?

_Table = new Dictionary<string, string>();

if (_Table.ContainsKey(projectName.ToLower()) == true)
    return _Table[projectName.ToLower()];
else
    return null;

You can try something like:

var key = _Table.Keys.FirstOrDefault(k => k.Contains(projectName.ToLower()));
if (key != null)
{
    return _Table[key];
}
else
{
    return null;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM