2017-06-19 68 views
0

我有两个字典。一个是实际的词典(词的键和定义的价值),另一个是在Word文件中存储在第二字典中的单词。比较两个字典c#并获得相同的密钥

//first dictionary 
    var xdoc = XDocument.Load("dicoFrancais.xml"); 
      var dico = xdoc.Root.Elements() 
           .ToDictionary(a => (string)a.Attribute("nom"), 
              a => (string)a.Element("DEFINITION")); 

//second dictionary 
Dictionary<string, string> motRap = new Dictionary<string, string>(); 
     Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application(); 
     Document document = application.Documents.Open("monfichiertxt.docx"); 


     int count = document.Words.Count; 
     for (int i = 1; i <= count; i++) 
     {     
      string text = document.Words[i].Text;     
      motRap.Add(text, "blabla");     
     } 
     // Close word. 
     application.Quit(); 

我想比较两个库的钥匙,并获得与第一字典的值相同的密钥,所以有我了,第三次字典与键和值。 我试过这个: var intersectMembers = dico.Keys.Intersect(motRap.Keys) .ToDictionary(t => t, t => dico[t]); 但它不起作用。 有人可以帮我, 谢谢。 (对不起,我的英文不太好)

+1

“但它不起作用”是什么意思?你得到了什么结果,你期望什么? – HimBromBeere

+0

第三个字典没有显示 – titi2fois

回答

0

我想比较两个字典的键值,并得到与第一个字典的值相同的键。

var thirdDictionary = dico 
    .Where(keyValue => motRap.Keys.Contains(keyValue.Key)) 
    .ToDictionary(keyValue => keyValue.Key, keyValue => keyValue.Value); 
相关问题