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 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);
}
}
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("Amount").Value.ToString());
//Current_Date = Convert.ToDateTime(xElement.Element("Current_Date").Value.ToString());
Current_Date = ((DateTime)xElement.Element("Current_Date")).Date;
}
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
2010-12-31T18:08:23.433+05:30
The date format is yyyy-mm-dd.
Now how should I write the following query to get all the submitted transaction details for current week as well as current month ?
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));
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.
The following code will give the current week summary :
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);
The Following code will give the current month summary
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);
OR both the queries can be wriiten for the current week of selected date & current month of selected date as follows
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);
}