我有一个xml,类似于:
<?xml version="1.0" encoding="utf-8"?>
<client>
<Customers>
<Customer>
<Customer_TIN>ABC</Customer_TIN>
<Cust_PDR>001</Cust_PDR>
<CurrentBusinessDate>06-13-2014</CurrentBusinessDate>
<Facilities>
<Facility>
<Cust_External_Id>123</Cust_External_Id>
<FacilityExpiryDate>2014-06-13</FacilityExpiryDate>
</Facility>
<Facility>
<Cust_External_Id>456</Cust_External_Id>
<FacilityExpiryDate>2014-06-14</FacilityExpiryDate>
</Facility>
</Facilities>
</Customer>
</Customers>
</client>
我正在使用LINQ to XML来选择Customers元素中的数据,但我需要只包含那些具有FacilityExpiryDate&gt;的设施。 CurrentBusinessDate。
我能够使用以下代码在Facility级别找到它:
(from i in xDoc.Descendants("Facility")
where
Convert.ToDateTime(i.Element("FacilityExpiryDate").Value) >
Convert.ToDateTime((from j in xDoc.Descendants("CurrentBusinessDate")
select (string)j).First().ToString())
select i).ToList();
但我想知道是否可以在客户级别开始解析并获得相同的结果。
答案 0 :(得分:1)
这应该有效:
xDoc.Descendants("Facility")
.Where(
x =>
(DateTime) x.Element("FacilityExpiryDate") >
(DateTime) x.Parent.Parent.Element("CurrentBusinessDate"));
或者:
xDoc.Descendants("Customer")
.SelectMany(
x =>
x.Elements("Facility")
.Where(
f =>
(DateTime) f.Element("FacilityExpiryDate") >
(DateTime) x.Element("CurrentBusinessDate")));