c# - 仅从 linq c#.net 中的第一个元素搜索值

标签 c# xml linq

我的问题出在哪里,请检查一下

<Uni_Details Uni_ID="U001" User_Mode="New">
  <Uni_Name>
    <Full_Name>Asian</Full_Name>
    <Short_Name />
  </Uni_Name>
  <Asc_Uni>
    <Asc_Details Asc_Name="Stafford">
      <Asc_Add>fsfadfsdfsdfsdf</Asc_Add>
      <Asc_Tel>+44 34234234234</Asc_Tel>
      <Asc_Country>United Kingdom</Asc_Country>
      <Asc_Web>www.ac.lk</Asc_Web>
    </Asc_Details>
  </Asc_Uni>
  <Branch>
    <Branch_Details Branch_Name="Head Office">
      <Branch_Add>dasdasdadsd</Branch_Add>
      <Branch_City>Colombo</Branch_City>
      <Branch_Country>Sri Lanka</Branch_Country>
      <Branch_Tel>+94 342432423</Branch_Tel>
    </Branch_Details>
    <Branch_Details Branch_Name="Access Tower">
      <Branch_Add>dfgdfgdfgdf</Branch_Add>
      <Branch_City>Colombo</Branch_City>
      <Branch_Country>Sri Lanka</Branch_Country>
      <Branch_Tel>+94 342432453</Branch_Tel>
    </Branch_Details>
  </Branch>
  <Uni_EMail>info@apiit.lk</Uni_EMail>
  <Uni_Web>www.apiit.lk</Uni_Web>
  <Course_Data>
    <Course_Details Course_ID="U001C001">
      <Course_Name>Computer Course</Course_Name>
      <Course_Qual>Pass O/L</Course_Qual>
      <Course_Dur>3 Months</Course_Dur>
      <Course_Content>
        <Course_Conent_List>C#.net</Course_Conent_List>
        <Course_Conent_List>VB.Net</Course_Conent_List>
        <Course_Conent_List>Design Practices</Course_Conent_List>
      </Course_Content>
      <Course_Fee>
        <Fee_Amount Amount_Currency="SL Rs.">5000.00</Fee_Amount>
        <Fee_Comment>Onwards</Fee_Comment>
      </Course_Fee>
      <Course_Desc>Learn Basic Programming practices</Course_Desc>
      <Course_Next>Next October</Course_Next>
      <Category_List>
        <Cat_ID>2</Cat_ID>
        <Cat_ID>1</Cat_ID>
      </Category_List>
    </Course_Details>
  </Course_Data>
</Uni_Details>
<Uni_Details Uni_ID="U002" User_Mode="New">
  <Uni_Name>
    <Full_Name>American College</Full_Name>
    <Short_Name>AC</Short_Name>
  </Uni_Name>
  <Asc_Uni>
    <Asc_Details Asc_Name="American College USA">
      <Asc_Add>fasdfsdfsdfsdsdfs</Asc_Add>
      <Asc_Tel>+94 112 230623</Asc_Tel>
      <Asc_Country>United States</Asc_Country>
      <Asc_Web>www.ac.lk</Asc_Web>
    </Asc_Details>
  </Asc_Uni>
  <Branch>
    <Branch_Details Branch_Name="Head Office">
      <Branch_Add>FFSDFASDFSDF</Branch_Add>
      <Branch_City>Colombo</Branch_City>
      <Branch_Country>Sri Lanka</Branch_Country>
      <Branch_Tel>+94 112 230623</Branch_Tel>
    </Branch_Details>
  </Branch>
  <Uni_EMail>info@ac.lk</Uni_EMail>
  <Uni_Web>www.ac.lk</Uni_Web>
  <Course_Data>
    <Course_Details Course_ID="U002C0001">
      <Course_Name>Computer Course</Course_Name>
      <Course_Qual>O/L Pass</Course_Qual>
      <Course_Dur>3 Months</Course_Dur>
      <Course_Content>
        <Course_Conent_List>C#.net</Course_Conent_List>
        <Course_Conent_List>VB.Net</Course_Conent_List>
        <Course_Conent_List>Design Practices</Course_Conent_List>
      </Course_Content>
      <Course_Fee>
        <Fee_Amount Amount_Currency="SL Rs.">5000</Fee_Amount>
        <Fee_Comment></Fee_Comment>
      </Course_Fee>
      <Course_Desc>Basics</Course_Desc>
      <Course_Next>Next October</Course_Next>
      <Category_List>
        <Cat_ID>1</Cat_ID>
      </Category_List>
    </Course_Details>
    <Course_Details Course_ID="U002C0002">
      <Course_Name>Accounting Course</Course_Name>
      <Course_Qual>O/L Pass</Course_Qual>
      <Course_Dur>6 Months</Course_Dur>
      <Course_Content>
        <Course_Conent_List>Accounts Basics</Course_Conent_List>
        <Course_Conent_List>Business Studies</Course_Conent_List>
        <Course_Conent_List>Acc Pack</Course_Conent_List>
      </Course_Content>
      <Course_Fee>
        <Fee_Amount Amount_Currency="SL Rs.">10000</Fee_Amount>
        <Fee_Comment></Fee_Comment>
      </Course_Fee>
      <Course_Desc>Basic Accounting</Course_Desc>
      <Course_Next>Next January</Course_Next>
      <Category_List>
        <Cat_ID>3</Cat_ID>
        <Cat_ID>2</Cat_ID>
      </Category_List>
    </Course_Details>
  </Course_Data>

</Uni_Details>

这是我的 XML 文件,我需要检索所有 cat_id 为 1 的数据

 var data = (from cats in data_file.Elements("Uni_Data").Elements("Uni_Details")
                where cats.Element("Course_Data").Element("Course_Details").Element("Category_List").Element("Cat_ID").Value == cat_id.ToString()
                select new
                {
                    uni_name = cats.Element("Uni_Name").Element("Full_Name").Value.ToString(),
                    uni_short = cats.Element("Uni_Name").Element("Short_Name").Value.ToString(),
                    price = cats.Element("Course_Data").Element("Course_Details").Element("Course_Fee").Element("Fee_Amount").Value.ToString()
                });

但是,如果我将 cat_id 1 作为我的第一个元素,如果它排在第二位,它就不会列出,所以这会起作用,所以请帮助我

最佳答案

问题出在你的 where 结束的方式上条款设置:Element("Cat_ID").Value == cat_id.ToString() .

具体来说,Element("Cat_ID")部分将检查第一个子“Cat_ID”元素。因此,如果 Cat_ID 1 是第二项,则不会找到它。相反,您需要检查所有子元素是否匹配。

var data = (from cats in data_file.Elements("Uni_Data").Elements("Uni_Details")
            let catId = cats.Element("Course_Data").Element("Course_Details").Element("Category_List").Elements("Cat_ID").FirstOrDefault(c => c.Value == cat_id.ToString())
            where catId != null
            select new
            {
                uni_name = cats.Element("Uni_Name").Element("Full_Name").Value.ToString(),
                uni_short = cats.Element("Uni_Name").Element("Short_Name").Value.ToString(),
                price = cats.Element("Course_Data").Element("Course_Details").Element("Course_Fee").Element("Fee_Amount").Value.ToString()
            });

查询使用 let存储 FirstOrDefault 结果的子句调用,它使用谓词来匹配条件。接下来,如果它不为空,则查询的其余部分将像以前一样工作。如果为 null,则表示未找到匹配项。

也就是说,您的查询有很多重复元素。它还使用 .Value.ToString()在一些地方,这是多余的,因为 Value属性已经返回一个字符串。我将按如下方式清理查询:

var data = from cats in data_file.Elements("Uni_Data").Elements("Uni_Details")
           let uniName = cats.Element("Uni_Name")
           let details = cats.Element("Course_Data").Element("Course_Details")
           let catId = details.Element("Category_List").Elements("Cat_ID").FirstOrDefault(c => c.Value == cat_id.ToString())
           where catId != null
           select new
           {
               uni_name = uniName.Element("Full_Name").Value,
               uni_short = uniName.Element("Short_Name").Value,
               price = details.Element("Course_Fee").Element("Fee_Amount").Value
           };

关于c# - 仅从 linq c#.net 中的第一个元素搜索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20087433/

相关文章:

c# - 在 C# 中获取 unicode 目录名称?

c# - C#中foreach循环中的计数器

c# - 在 Entity Framework 上运行嵌套查询时出现"Value cannot be null. Parameter name: source"

c# - 如何将 XML "row"加载到列表框 C# 的索引中

java - 如何从 Android 上的 https url 获取 xml?

linq - Entity Framework - 从 ObjectContext 查询与从导航属性查询

c# - 显示带有前导零的数字文本框值

java - 在 Java 中使用 XML 解析器创建文档变量时出错

c# - LINQ to Entities 仅支持无参数构造函数和初始值设定项。 (ASP.NET 网络 API)

c# - 在 if 语句之前初始化一个对象