如何使用LINQ to XML查询日期时间值? [英] How to query with the datetime value using LINQ to XML?

查看:76
本文介绍了如何使用LINQ to XML查询日期时间值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Silverlight中开发Window Phone 7应用程序.我是新来的.我还是LINQ to XML的新手.在我的应用程序中,用户选择日期&提交一些交易细节到应用程序中.详细信息将存储在XML文件中.我在应用程序中使用自定义日期控件进行日期选择,如下所示

I am developing window phone 7 application in silverlight. I am new to the silverlight. I am also new to LINQ to XML. In my application the user select the date & submit some transaction details into the application. The details gets stored in XML File. I am using the custom date control in my application for the date selection as follows

 private void DatePicker_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
        {
            AppObj = Application.Current as App;
            AppObj.date = (DateTime)EntryDate.Value;         

        }

然后将AppObj.date的值存储在XML文件中.有时我使用DateTime.Now将日期存储在XML文件中.现在,我想通过LINQ to XML查询生成提交的交易明细的报告.我想为今天的日期,当前的周和周生成报告.这个月.对于今天的日期报告,我使用以下代码

Then the value of AppObj.date gets stored in the XML file. Sometimes I use the DateTime.Now to store the date in the XML File. Now I want to generate the report of submitted transaction details by querying through LINQ to XML. I want to generate the report for today's date, current week & current month. For today's date report I am using the following code

public void GetTransactionObjects(String strXMLFile, DateTime VDateTime)
        {            
            XDocument doc = null;
            XMLFileManager XMLDocObj = new XMLFileManager();
            doc = XMLDocObj.LoadXMLFile(strXMLFile);
            var vTransaction = from s in doc.Descendants("Transaction")
                               .Where(x => x.Element("Current_Date").Value == VDateTime.ToShortDateString())
                               select new Transaction(s);
            this.Clear();
            AddRange(vTransaction);           

        }

Transaction类包含以下构造函数.

The Transaction class contains the following constructor.

public Transaction(XElement xElement)
        {
            Transaction_ID = Convert.ToInt32(xElement.Element("Transaction_ID").Value.ToString());
            TransactionType_ID = Convert.ToInt32(xElement.Element("TransactionType_ID").Value.ToString());
            Alphabet_ID = Convert.ToInt32(xElement.Element("Alphabet_ID").Value.ToString());
            ID = Convert.ToInt32(xElement.Element("ID").Value.ToString());
            SubCategory_ID = Convert.ToInt32(xElement.Element("SubCategory_ID").Value.ToString());
            Item_ID = Convert.ToInt32(xElement.Element("Item_ID").Value.ToString());
            Currency_ID = Convert.ToInt32(xElement.Element("Currency_ID").Value.ToString());
            InputTypeMethod_ID = Convert.ToInt32(xElement.Element("InputTypeMethod_ID").Value.ToString());          
            Principle = Convert.ToInt32(xElement.Element("InputTypeMethod_ID").Value.ToString());
            Interest = Convert.ToInt32(xElement.Element("Interest").Value.ToString());
            ROI = Convert.ToInt32(xElement.Element("InputTypeMethod_ID").Value.ToString());
            Amount = Convert.ToInt32(xElement.Element("InputTypeMethod_ID").Value.ToString());
            Current_Date = Convert.ToDateTime(xElement.Element("Current_Date").Value.ToString());
        }

在XML文件中,该值存储为日期&.时间.值存储如下

In the XML File the value gets stored for date & time. The value gets stored as follows

<Transactions>
  <Transaction>
    <Transaction_ID>0</Transaction_ID>
    <TransactionType_ID>0</TransactionType_ID>
    <Alphabet_ID>3</Alphabet_ID>
    <ID>0</ID>
    <SubCategory_ID>0</SubCategory_ID>
    <Item_ID>0</Item_ID>
    <Currency_ID>3</Currency_ID>
    <InputTypeMethod_ID>0</InputTypeMethod_ID>
    <Principle>0</Principle>
    <Interest>0</Interest>
    <ROI>0</ROI>
    <Amount>5000</Amount>
    <Current_Date>2010-12-31T18:08:23.433+05:30</Current_Date>
  </Transaction>
</Transactions>

查看节点

<Current_Date>2010-12-31T18:08:23.433+05:30</Current_Date>

日期格式为yyyy-mm-dd.

The date format is yyyy-mm-dd.

现在我应该如何编写以下查询来获取当天的所有已提交交易明细?

Now how should I write the following query to get all the submitted transaction details for today's date ?

var vTransaction = from s in doc.Descendants("Transaction")
                                   .Where(x => x.Element("Current_Date").Value == VDateTime.ToShortDateString())
                                   select new Transaction(s); 

类似地,我应该如何编写查询以获取当前周&的所有交易明细?这个月?您能否提供任何可解决上述问题的代码或链接?如果我做错了什么,请引导我.

Similarly how should I write the query to get all the transaction details for the current week & current month? Can you please provide me any code or link through which I can resolve the above issue ? If I am doing anything wrong then please guide me.

推荐答案

不要在LINQ to XML中使用Convert.ToDateTimeToShortDateString等.使用XAttributeXElement中已经存在的转换.例如:

Don't use Convert.ToDateTime or ToShortDateString etc with LINQ to XML. Use the conversions which already exist in XAttribute and XElement. For example:

DateTime today = DateTime.Today;
var query = doc.Descendants("Transaction")
               .Where(x => ((DateTime) x.Element("Current_Date")).Date == today)
               .Select(x => new Transaction(s));

(您也应该在Transaction构造函数中使用转换运算符.)

(You should use the conversion operator in your Transaction constructor too.)

这篇关于如何使用LINQ to XML查询日期时间值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆