Linq to XML 查询日期间隔
我正在使用一个具有以下格式的大型 xml 文件:
<Sales>
<Sale>
<Date>04/20/2010</Date>
<Time>18:17:29</Time>
<Id>P00000001</Id>
<Name>Prod Name</Name>
<Description>Prod Desc</Description>
<Category>Prod Category</Category>
<Subcategory>Prod Subcategory</Subcategory>
<Size>Prod Size</Size>
<Price>25</Price>
<Image>image path</Image>
</Sale>
<Sales>
我的目标是显示价格总和并计算一段时间内销售的商品数量:
XDocument xmlDoc = XDocument.Load("Data.xml");
DateTime date = new DateTime();
var queryWeek = from sale in xmlDoc.Descendants("Sale")
where ((sale.Element("Date")>= date.Date.AddDays(-7)) && ((sale.Element("Date")<= date.Date())))
select sale.Element("Price");
Console.WriteLine("",queryWeek);
调试器抱怨: 错误 1 运算符“>=”不能应用于
'System.Xml.Linq.XElement' and 'System.DateTime'
Error 2 Non-invocable member 'System.DateTime.Date' cannot be used like a method.
help 类型的操作数 德文
i am using a large xml file having in the following format:
<Sales>
<Sale>
<Date>04/20/2010</Date>
<Time>18:17:29</Time>
<Id>P00000001</Id>
<Name>Prod Name</Name>
<Description>Prod Desc</Description>
<Category>Prod Category</Category>
<Subcategory>Prod Subcategory</Subcategory>
<Size>Prod Size</Size>
<Price>25</Price>
<Image>image path</Image>
</Sale>
<Sales>
my goal is to display to sum the prices and count items sold for a period of time:
XDocument xmlDoc = XDocument.Load("Data.xml");
DateTime date = new DateTime();
var queryWeek = from sale in xmlDoc.Descendants("Sale")
where ((sale.Element("Date")>= date.Date.AddDays(-7)) && ((sale.Element("Date")<= date.Date())))
select sale.Element("Price");
Console.WriteLine("",queryWeek);
debugger complains:
Error 1 Operator '>=' cannot be applied to operands of type
'System.Xml.Linq.XElement' and 'System.DateTime'
Error 2 Non-invocable member 'System.DateTime.Date' cannot be used like a method.
help please
devin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在尝试比较 DateTime 和 XElement。您需要将 XElement 更改为 DateTime 进行比较:
DateTime.Parse(sale.Element("Date").Value) >= date.Date.AddDays(-7)
。You are attempting to compare a DateTime and the XElement. You need to change the XElement to a DateTime for comparison:
DateTime.Parse(sale.Element("Date").Value) >= date.Date.AddDays(-7)
.您可以使用此技术将 XElement 显式转换为 DateTime(在 .NET 3.5 和 4.0 中):
http://msdn.microsoft.com/en-us/library/bb343203.aspx
You may be able to explicitly convert the XElement to a DateTime using this technique (in .NET 3.5 & 4.0):
http://msdn.microsoft.com/en-us/library/bb343203.aspx