将foreach循环转换为LINQ查询 [英] Convert foreach loop into LINQ query

查看:114
本文介绍了将foreach循环转换为LINQ查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将foreach循环转换为LINQ?

How to convert foreach loop into LINQ?

foreach (XmlNode item in xmlnode)
               {
                   if (strArr.Contains(item.Attributes.GetNamedItem(MspecAttribute).Value))//search the XML with the values in the array
                   {
                       //add the XML values to the datatable
                       dt.Rows.Add(item.Attributes.GetNamedItem(MspecAttribute).Value, item.Attributes.GetNamedItem(XmlNameAttribute).Value);
                   }
               }





我有一个字符串数组,其中包含我正在搜索XML的值文件并获取值。



I have a string array with the values i am searching a XML file and fetching the values.

推荐答案

您好



请尝试以下代码:



Hi

Try below code:

XElement x = XElement.Load(@"Items.xml");
                string strCompare = "Test";
                                var element = from XMLelement in x.Elements("Item")
                              where (string) XMLelement.Attribute("Value")==strCompare
                              select XMLelement;



< br $>


谢谢

Harshad




Thanks
Harshad


这篇关于将foreach循环转换为LINQ查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆