1

我正在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 文件中,值被存储为日期和时间。该值按如下方式存储

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

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

4

2 回答 2

0

DateTime对象有一个属性Month,您应该可以按月过滤。一周你可以GetWeekOfYearCalendar课堂上使用,阅读这个链接:http: //msdn.microsoft.com/en-us/library/system.globalization.calendar.getweekofyear.aspx

于 2011-01-04T06:52:39.923 回答
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);
}
于 2011-01-06T08:22:38.357 回答