【问题标题】:Compare dictionary <string,List<string>> single value with a list of values c#比较字典 <string,List<string>> 单个值与值列表 c#
【发布时间】:2014-04-25 09:15:20
【问题描述】:

用相同的键解析xml和表单字典以获取多个值

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

doc.Load(confidencethresholdFilePath + "\\input.xml");
// XmlNodeList nodes = doc.DocumentElement.SelectNodes("/root/file");
doc.Normalize();
XmlNodeList nList = doc.GetElementsByTagName("key");
for (int temp = 0; temp < nList.Count; temp++)
{
    string keyvalue = "";
    XmlNode nNode = nList.Item(temp);
    List<String> main = new List<string>();
    ArrayList arrayList = new ArrayList(main);
    XmlElement ele = (XmlElement)nNode;
    keyvalue = ele.GetAttribute("value");
    for (int i = 0; i < ele.ChildNodes.Count; i++)
    {
        if (ele.ChildNodes[i].ChildNodes.Count == 1)
        {
            Double sec = Double.Parse(ele.ChildNodes[i].InnerText);
            int seg = TimeToSegment(sec);
            Console.WriteLine("" + seg);
            main.Add(Convert.ToString(seg));
        }
    }
    dictionary.Add(keyvalue, main);
}

我想与单个值比较,但它显示错误 dictionary.ContainsValue(s.sname) 无效参数

【问题讨论】:

    标签: c# .net wpf c#-4.0 dictionary


    【解决方案1】:

    我不确定您要检查什么,但如果您尝试匹配键,请执行以下操作:

    dictionary.ContainsKey(s.sname);
    

    或者你可以试试:

    IEnumerable<KeyValuePair<string,List<string>>> ienKeyVals = dictionary.Where(x => x.Value.Contains(s.sname));
    

    用相同的键解析xml和表单字典以获取多个值

    字典不能包含重复的键。无法将重复项添加到 Dictionary - 另一种方法是使用 Lookup 类。

    Enumerable.ToLookup Method

    从 IEnumerable 创建通用查找。

    【讨论】:

      猜你喜欢
      • 2014-12-03
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      • 1970-01-01
      • 1970-01-01
      • 2011-12-11
      相关资源
      最近更新 更多