【发布时间】:2019-11-21 12:08:57
【问题描述】:
我有一个 Dictionary<string, string> 对象,其中存储的值如下所示:
examplePlanet : defaultText0
examplePlanet* : defaultText1
examplePlanet** : defaultText2
examplePlanetSpecificlocationA : specificAText0
examplePlanetSpecificlocationA* : specificAText1
examplePlanetSpecificlocationB : specificBText
我有一个字符串 filter 匹配这些键之一或者是键的子集。
该过滤器的形式为planetLocation,可拆分为planet和location。
我的目标是以这种方式创建过滤器匹配的值列表:如果字典中存在planetLocation,则将其值以及键匹配但具有额外*的所有值添加到列表中。
如果planetLocation 不存在,则仅添加键与过滤器的planet 部分匹配的值(可能有额外的*)。
基本上,我希望过滤器与键尽可能匹配的所有值。
例子:
examplePlanetSpecificlocationA 给[specificAText0, specificAText1]examplePlanetSpecificlocationB 给[specificBText]examplePlanetSpecificlocationC 给[defaultText0, defaultText1, defaultText2]
我已经尝试过(以及其他不起作用的方法):
private List<string> filteredResults;
///<summary>Filters dictionaries and returns a list of values</summary>
private List<string> GetFilteredResults(Dictionary<string, string> inputdictionary, string filter)
{
List<string> _filteredResults = new List<string>();
foreach (KeyValuePair<string, string> entry in inputdictionary)
{
if (entry.Key.Contains(filter))
{
_filteredResults.Add(entry.Value);
}
}
return _filteredResults;
}
public void main()
{
//stuff happens here that assigns a value to filterPlanet and filterLocation
filteredResults = new List<string>();
filteredResults = GetFilteredResults(exampledictionary, filterPlanet + filterLocation);
if (filteredResults.Count == 0)
{
filteredResults = GetFilteredResults(exampledictionary, filterPlanet);
}
//do stuff with the filtered results
}
这几乎可行,但返回键包含 filterPlanet 的所有值,而不仅仅是 filterPlanet 本身加上可能的 *。我不知道如何让这个函数做我想做的事,即使它以某种方式工作,我相信还有比这更有效的过滤方式。你能帮帮我吗?
【问题讨论】:
-
为什么“examplePlanetSpecificlocationC”匹配“examplePlanet”?为什么“examplePlanetSpecificlocationA”匹配“examplePlanetSpecificlocationA*”?
-
@canton7 examplePlanetSpecificlocationC 在字典中不存在,因此应返回默认值,该值存储在具有相同名称但没有位置位的键中。编辑:examplePlanetSpecificlocationA 始终与 examplePlanetSpecificlocationA* 匹配,因为第一个是后者的子集,仅添加了星号。字典不能有两次相同的键,这似乎是一种解决方法。
-
对,但是“examplePlanet”不以“”结尾?为什么“examplePlanetSpecificlocationA”匹配“examplePlanetSpecificlocationA”,如果它已经在“examplePlanetSpecificlocationA”中有一个特定的匹配?
-
如果你能详细写出你的算法,使用多级列表或流程图会更好。详细说明应该采取哪些步骤,以及在所有情况下应该发生什么。这也将帮助您实现它。
-
我想建议使用字符串字典不是解决此问题的正确方法。它必须是一个字符串字典(例如,您是否从外部源获取此确切数据)还是可以使用不同的数据结构?
标签: c# dictionary filter