小编lio*_*ior的帖子

使用LINQ返回的对象

我正在使用LINQ从XML文件中查找对象.找到对象后,我想打印它的细节,但我不确定如何使用我找到的对象.

这是我的代码:

var apartmentExist =
    from apartment1 in apartmentXml.Descendants("Apartment")
    where (apartment1.Attribute("street_name").Value == newApartment.StreetName) &&
          (apartment1.Element("Huose_Num").Value == newApartment.HouseNum.ToString())
    select apartment1.Value;
if (apartmentExist.Any() == false)
{
    Console.WriteLine("Sorry, Apartment at {0} or at num {1}", newApartment.StreetName,
        newApartment.HouseNum);

}
else
{

    //print the details of apartment1
}
Run Code Online (Sandbox Code Playgroud)

我的XML是:

<?xml version="1.0" encoding="utf-8"?>
<Apartments>
  <Apartment street_name="sumsum">
    <Huose_Num>13</Huose_Num>
    <Num_Of_Rooms>4</Num_Of_Rooms>
    <Price>10000</Price>
    <Flags>
      <Elevator>true</Elevator>
      <Floor>1</Floor>
      <parking_spot>true</parking_spot>
      <balcony>true</balcony>
      <penthouse>true</penthouse>
      <status_sale>true</status_sale>
    </Flags>
  </Apartment>
</Apartments>
Run Code Online (Sandbox Code Playgroud)

c# linq

0
推荐指数
1
解决办法
98
查看次数

标签 统计

c# ×1

linq ×1