如何从linq查询中添加项目到字典?

Bum*_*mba -2 c# linq dictionary

我尝试检查一个XML文件,节点disp-formula具有属性" ID "及属性包含类似值deqnX-Y其中两个XY是整数,如果发现匹配,然后在词典中增加他们在下面的方式

Key                             Value
"rid="deqnX""               "rid="deqnX-Y""
"rid="deqnX+1""             "rid="deqnX-Y""
...  ...
"rid="deqnY""               "rid="deqnX-Y""

incrementing value of X by 1 till it reaches Y
Run Code Online (Sandbox Code Playgroud)

我已经尝试了下面的代码,但卡在中途,我无法弄清楚该做什么.

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

            XDocument doc = XDocument.Load(@"D:\Practice\test.xml",LoadOptions.PreserveWhitespace);
            var x =from y in doc.Descendants("disp-formula")
                   where y.Attribute("id").Value.Contains(@"deqn(\d+)-(\d+)")
                   select y.Attribute("id");
            foreach (var item in x)
            {

                dict.Add(item);
            }
Run Code Online (Sandbox Code Playgroud)

这是一个示例xml文件

<?xml version="1.0" encoding="UTF-8"?>
<article article-type="research">
<front>
<journal-meta>
<issn pub-type="paper">0327-286X</issn>
<publisher>
<publisher-name>IEEE</publisher-name>
</publisher>
</journal-meta>
<article-meta>
<article-id pub-id-type="doi">10.1245/11.202136</article-id>
<title-group>
<article-title>Dragon Ball Super popularity in USA</article-title>
</title-group>
</article-meta>
</front>
<body>
<sec id="S1">
<label>1.</label>
<p>....
<disp-formula id="deqn1">
...
</disp-formula>
</p>
</sec>
<sec id="S2">
<label>2.</label>
<p>...
<disp-formula id="deqn2-6">
...
</disp-formula></p>
<p>...
<disp-formula id="deqn7">
...
</disp-formula>
</p>
<p><disp-formula id="deqn8-10">
...
</disp-formula></p>
</sec>
</body>
</article>
Run Code Online (Sandbox Code Playgroud)

这应该是运行程序后的字典项

Key                             Value
"rid="deqn2""               "rid=""deqn2-6""
"rid="deqn3""               "rid=""deqn2-6""
"rid="deqn4""               "rid=""deqn2-6""
"rid="deqn5""               "rid=""deqn2-6""
"rid="deqn6""               "rid=""deqn2-6""
"rid="deqn8""               "rid=""deqn8-10""
"rid="deqn9""               "rid=""deqn8-10""
"rid="deqn10""              "rid=""deqn8-10""
Run Code Online (Sandbox Code Playgroud)

Mik*_*Mat 5

这个问题包括:

  1. 找到与正则表达式匹配的元素
  2. 从匹配的字符串中获取X和Y.
  3. 增加X直到Y并将它们存储在Dictionary中

var dict = new Dictionary<string, string>();
var xdoc = XDocument.Load(@"D:\Practice\test.xml", LoadOptions.PreserveWhitespace);
var regex = new Regex(@"deqn(\d+)-(\d+)");

// Get matches by the regex
var matches = from dispFormula in xdoc.Descendants("disp-formula")
                select regex.Match(dispFormula.Attribute("id").Value);
// We want only successes
matches = matches.Where(match => match.Success);

foreach (var match in matches)
{
    // If input string is "deqn2-6", 
    //   match.Groups[0].Value = "deqn2-6", 
    //   match.Groups[1].Value = "2", 
    //   match.Groups[2].Value = "6", so
    int x = int.Parse(match.Groups[1].Value);
    int y = int.Parse(match.Groups[2].Value);

    // Now we have to increment x until y
    for (int i = 0; x + i <= y; i++)
    {
        dict.Add($"deqn{x + i}", $"deqn{x}-{y}");
    }
}

foreach (var entry in dict)
{
    Console.WriteLine($"Key={entry.Key}, Value={entry.Value}");
}
Run Code Online (Sandbox Code Playgroud)

这输出

Key=deqn2, Value=deqn2-6
Key=deqn3, Value=deqn2-6
Key=deqn4, Value=deqn2-6
Key=deqn5, Value=deqn2-6
Key=deqn6, Value=deqn2-6
Key=deqn8, Value=deqn8-10
Key=deqn9, Value=deqn8-10
Key=deqn10, Value=deqn8-10
Run Code Online (Sandbox Code Playgroud)