如何查询当前周和&使用LINQ to XML的当前月报告?

时间:2011-01-04 06:43:49

标签: c# silverlight datetime windows-phone-7 summary

我正在开发silverlight中的window phone 7应用程序。我是银光的新手。我也是LINQ to XML的新手。在我的应用程序中,用户选择日期&将一些交易细节提交到应用程序中。细节存储在XML文件中。我在我的应用程序中使用自定义日期控件进行日期选择,如下所示

        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生成提交的事务详细信息的报告。我想为今天的日期,当前周和&生成报告。这个月。对于今天的日期报告,我使用以下代码

public class TransactionList : List<Transaction>
{
    public void GetTransactionObjects(String strXMLFile, int Currency_ID, int TransactionType_ID)
    {            
        XDocument doc = null;
        XMLFileManager XMLDocObj = new XMLFileManager();
        doc = XMLDocObj.LoadXMLFile(strXMLFile);

        DateTime today = DateTime.Today;
        var vTransaction = doc.Descendants("Transaction")
                          .Where(x => ((DateTime)x.Element("Current_Date")).Date == today) 
                          .Where(x => x.Element("TransactionType_ID").Value == TransactionType_ID.ToString())
                          .Where(x => x.Element("Currency_ID").Value == Currency_ID.ToString())                              
                           .Select(x => new Transaction(x));
        this.Clear();
        AddRange(vTransaction);           

    }
}

Transaction类包含以下构造函数。

    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("Amount").Value.ToString());
        //Current_Date = Convert.ToDateTime(xElement.Element("Current_Date").Value.ToString());
        Current_Date = ((DateTime)xElement.Element("Current_Date")).Date;
    }

在XML文件中,值存储为date&amp;时间。该值存储如下

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

查看节点

2010-12-31T18:08:23.433 + 05:30

日期格式为yyyy-mm-dd。

现在我应该如何编写以下查询以获取当前周和当月的所有提交的交易详情?

var vTransaction = doc.Descendants("Transaction")
                      .Where(x => ((DateTime)x.Element("Current_Date")).Date == today) 
                      .Where(x => x.Element("TransactionType_ID").Value == TransactionType_ID.ToString())
                      .Where(x => x.Element("Currency_ID").Value == Currency_ID.ToString())                              
                      .Select(x => new Transaction(x));

您能否提供我可以解决上述问题的任何代码或链接?如果我做错了什么,请指导我。

2 个答案:

答案 0 :(得分:0)

DateTime对象有一个属性Month,您应该可以按月过滤。对于一周,您可以使用GetWeekOfYear课程中的Calendar,阅读此链接:http://msdn.microsoft.com/en-us/library/system.globalization.calendar.getweekofyear.aspx

答案 1 :(得分:0)

以下代码将提供当前周摘要:

DateTime startDate = DateTime.Today.Date.AddDays(-(int)DateTime.Today.DayOfWeek), // prev sunday 00:00
endDate = startDate.AddDays(7); // next sunday 00:00

var vTransaction = from x in doc.Descendants("Transaction")
                               where ((DateTime)x.Element("Current_Date")).Date >= startDate
                               && ((DateTime)x.Element("Current_Date")).Date < endDate
                               where x.Element("TransactionType_ID").Value == TransactionType_ID.ToString() 
                               select new Transaction(x); 

以下代码将提供当前月份摘要

int CurrentYear = DateTime.Today.Year;
int CurrentMonth = DateTime.Today.Month;

DateTime startDate = new DateTime(CurrentYear, CurrentMonth, 1);
DateTime endDate = startDate.AddMonths(1).AddMinutes(-1);

var vTransaction = from x in doc.Descendants("Transaction")
                                   where ((DateTime)x.Element("Current_Date")).Date >= startDate
                                   && ((DateTime)x.Element("Current_Date")).Date < endDate
                                   where x.Element("TransactionType_ID").Value == TransactionType_ID.ToString()
                                   select new Transaction(x);

或者两个查询都可以在所选日期的当前周内查看。所选日期的当前月份如下

public void GetCurrentWeekSummary(String strXMLFile, int TransactionType_ID, DateTime selectedDate)
{
                XDocument doc = null;
                XMLFileManager XMLDocObj = new XMLFileManager();
                doc = XMLDocObj.LoadXMLFile(strXMLFile);

                DateTime startDate = selectedDate.Date.AddDays(-(int)selectedDate.DayOfWeek), // prev sunday 00:00
                endDate = startDate.AddDays(7); // next sunday 00:00

                var vTransaction = from x in doc.Descendants("Transaction")
                           where ((DateTime)x.Element("Current_Date")).Date >= startDate
                           && ((DateTime)x.Element("Current_Date")).Date < endDate
                           where x.Element("TransactionType_ID").Value == TransactionType_ID.ToString() 
                           select new Transaction(x);
}



public void GetCurrentMonthSummary(String strXMLFile, int TransactionType_ID, DateTime selectedDate)
{
                XDocument doc = null;
                XMLFileManager XMLDocObj = new XMLFileManager();
                doc = XMLDocObj.LoadXMLFile(strXMLFile);

                int CurrentYear = selectedDate.Year;
                int CurrentMonth = selectedDate.Month;

                DateTime startDate = new DateTime(CurrentYear, CurrentMonth, 1);
                DateTime endDate = startDate.AddMonths(1).AddMinutes(-1);


                var vTransaction = from x in doc.Descendants("Transaction")
                               where ((DateTime)x.Element("Current_Date")).Date >= startDate
                               && ((DateTime)x.Element("Current_Date")).Date < endDate
                               where x.Element("TransactionType_ID").Value == TransactionType_ID.ToString()
                               select new Transaction(x);
}