DBEntities context = new DBEntities();
int currentyear = Convert.ToInt32(NextYear.SelectedItem.Value);
var results = from c in context.Tables
where ( c.PRODNo == PRODNo.Text
&& c.Period.Year.Equals(currentyear)
)
select c;
DBEntities context = new DBEntities();
int currentyear = Convert.ToInt32(NextYear.SelectedItem.Value);
var results = from c in context.Tables
where ( c.PRODNo == PRODNo.Text
&& c.Period.Year.Equals(currentyear)
)
select c;
Thanks a lot for the quick response, the && seems OK, but the "Year" did not pass compiler, I have red line under Year.
currentYear is integer of year such as "2013", Period is DateTime field in SQL table which saved as DateTime datatype, I need the Year partion to conpare with currentYear.
It seems I have to change the syntax of Year, I also tried Year(c.Period).equals(currentyear), this does not work as well.
Peter Cong
Member
527 Points
681 Posts
How to add this line of code to my LINQ?
Jan 07, 2013 12:23 AM|LINK
Hi, I have following codes in my LINQ.
int currentyear = Convert.ToInt32(NextYear.SelectedItem.Value); //this is a dropdown list which returns year such as 2013.
DBEntities context = new DBEntities();
var result = from c in context.Tables
where c.PRODNo == PRODNo.Text
//The period is a datetime field in SQL table, I need the follow codes to be added in the query.
// and Year(c.Period) == currentyear //Anybody knows how to add this line of code?
select c;
if (result.Any()) //I also need to calculate the total of amount column in the collection returned
{
//how to get the toal of amount column(field) here
Table1 newresult=Subtotal(result.Amount); //I need something like this to calculate the total of amount column in the return collection of table rows.
int total =
}
Thanks a lot
kevinstone
Member
236 Points
48 Posts
Re: How to add this line of code to my LINQ?
Jan 07, 2013 01:20 AM|LINK
is your c.Period of type datetime?
try this: and c.Period.Year == currentyear
var columns = result.GetProperties();
int numberOfColumns = columns.Length;
Peter Cong
Member
527 Points
681 Posts
Re: How to add this line of code to my LINQ?
Jan 07, 2013 01:50 AM|LINK
Thanks a lot for the quick response, yes, Period is datetime datatype.
But, this code doesnot compile,
and c.Period.Year == currentYear
I do not know what is the purpose for your rest of codes? please read my post to understand my question.
var columns = result.GetProperties();
int numberOfColumns = columns.Length;
Anyway, thank you so much for your help,
.NetBoy
Member
439 Points
115 Posts
Re: How to add this line of code to my LINQ?
Jan 07, 2013 05:32 AM|LINK
DBEntities context = new DBEntities(); int currentyear = Convert.ToInt32(NextYear.SelectedItem.Value); var results = from c in context.Tables where ( c.PRODNo == PRODNo.Text && c.Period.Year.Equals(currentyear) ) select c;Peter Cong
Member
527 Points
681 Posts
Re: How to add this line of code to my LINQ?
Jan 07, 2013 12:46 PM|LINK
Thanks a lot for the quick response, the && seems OK, but the "Year" did not pass compiler, I have red line under Year.
currentYear is integer of year such as "2013", Period is DateTime field in SQL table which saved as DateTime datatype, I need the Year partion to conpare with currentYear.
It seems I have to change the syntax of Year, I also tried Year(c.Period).equals(currentyear), this does not work as well.
Any idea how to use Year in c.Period.Year part?
Thanks again.
.NetBoy
Member
439 Points
115 Posts
Re: How to add this line of code to my LINQ?
Jan 07, 2013 12:53 PM|LINK
Try:
Peter Cong
Member
527 Points
681 Posts
Re: How to add this line of code to my LINQ?
Jan 08, 2013 12:21 AM|LINK
still does not pass the compile on "Year", (there is red under line on "Year")
Pengzhen Son...
Star
8208 Points
846 Posts
Microsoft
Re: How to add this line of code to my LINQ?
Jan 12, 2013 06:22 AM|LINK
Hi,
You can try converting 'currentyear' to Date type:
string currentyear = NextYear.SelectedItem.Value;
DateTime dt = Convert.ToDateTime(currentyear);
then,
c.Period.Year==dt.Year
Feedback to us
Develop and promote your apps in Windows Store