How to query with the datetime value using LINQ to XML ?

Posted by Shailesh Jaiswal on Stack Overflow See other posts from Stack Overflow or by Shailesh Jaiswal
Published on 2010-12-31T12:50:13Z Indexed on 2010/12/31 12:54 UTC
Read the original article Hit count: 360

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;         

        }

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 this purpose 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);           

        }

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());
        }

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

0 0 3 0 0 0 3 0 0 0 0 5000 2010-12-31T18:08:23.433+05:30

Look at the node

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

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about Silverlight