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

仅从linq c#net中的第一个元素搜索值,c#,xml,linq,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_A

我在哪里有问题,请检查这个

<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>

但是,如果我将cat_id 1作为我的第一个元素,如果它位于第二位,它将不会列出,那么请帮助我解决这个问题。问题在于
where
子句的结尾设置方式:
元素(“cat_id”)。Value==cat_id.ToString()

具体来说,
元素(“Cat\u ID”)
部分将检查第一个子元素“Cat\u 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,则查询的其余部分与之前一样工作。如果为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
           };

非常感谢你,艾哈迈德·马格德先生,这是重点。再次感谢你
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
           };