How to query with the datetime value using LINQ to

2019-06-02 08:59发布

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

        }

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

<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>

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.

1条回答
聊天终结者
2楼-- · 2019-06-02 09:06

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

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

查看更多
登录 后发表回答