budgetAllocated.Where(a => Convert.ToDateTime(a.DateAllocated).Month == DateTime.Today.Month); |
But I keep getting the Error "System.Convert:ToDateTime(object) not supported on server side.
Is there another way I can get this done. HELP!!!
8 Answers, 1 is accepted
var budgetAllocated = from a in _scope.Extent<BudgetDetail>() |
where(((DateTime)a.DateAllocated).Month == DateTime.Today.Month && |
((DateTime)a.DateAllocated).Year == DateTime.Today.Year) |
select a; |
This worked.
You have found the right solution. Calling outer methods in Linq queries is not yet supported.
Best wishes,
Alexander
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
As a workaround in this case you could use the SQL<T> extension method that OpenAccess provides.
Here is an example how to achieve that for MSSQL using the convert function:
var result = (from p
in
context.MyClasses
where
"{0} < convert(datetime, {1})"
.SQL<
bool
>(p.DateTimeField, p.StringField)
select p).ToList();
This should work for other backends as well, just the SQL in the string literal would be different.
Hope that helps.
All the best,
Alexander
the Telerik team
hi
I also have a problem I want my app to give me the expiry date in two months advance from the current date but I want that notification to stay there until the last date which will be an Expiry date .my query does work but the notification doesn't last till the last day of Expiry date please help
here is my query
var query = (from b in con.tblNewMachines
where b.Machine_Exp_Date >= DateTime.Now.AddMonths(2) &&
b.Machine_Exp_Date < DateTime.Now.AddMonths(3)
select new
{
b.Machine_Tag,
b.Machine_Type,
b.Machine_Exp_Date
});
GridExpMachines.DataSource = query;
GridExpMachines.DataBind();
I am afraid I haven't completely understood your problem. Does your query run without any error but doesn't return the desired result? Can you ensure that the datetime values in the database are in the same timezone as specified in the condition (and not stored as UTC)?
Can you provide some more details about the problem.
Regards,
Ady
Telerik
Hi Ady
ok lets me put it simple this time I want to show machines to be expire in two months but I want those machines to stay in my RadGrid untill the last day or until they Expire complete then Automatically they will be moved to another grid of machines that are Expired . hope you will understand me now but don't worry about those that are already expired that one is already sorted
From what I understand you should be querying for all objects with an expiry date within the next 2 months.
I would write the query as follows -
01.
var query = (from b
in
con.tblNewMachines
02.
where b.Machine_Exp_Date >= DateTime.Now &&
03.
b.Machine_Exp_Date <= DateTime.Now.AddMonths(2)
04.
select
new
05.
{
06.
b.Machine_Tag,
07.
b.Machine_Type,
08.
b.Machine_Exp_Date
09.
});
This would give you all the objects that expire within the next 2 months and would continue to return the object till you reach the expiry date.
Hope this is what you want.
Do get back in case you need further assistance.
Regards,
Ady
Telerik