Reputation: 135
as follow my problem:
I have a table with all nvarchar columns and I would like to load only the records from the current day, like below:
var query = from item in dataContext.Items
orderby item.CDATE_IUD descending
where item.QV_USER == user
where item.QV_EXT == ext
where item.ACTIVE == 1
where DbFunctions.DiffDays(Convert.ToDateTime(item.CDATE_IUD), DateTime.Now.Date) == 0
select item;
I get an error that ToDateTime is not recognized in LINQ.
My desired output:
Input:
Any help would be appreciated.
Update:
This works for me:
string today = DateTime.Now.ToString("dd.MM.yyyy");
var query = from item in dataContext.Items
orderby item.CDATE_IUD descending
where item.QV_USER == user
where item.QV_EXT == ext
where item.ACTIVE == 1
where item.CDATE_IUD.Substring(0,10) == today
select item;
Upvotes: 1
Views: 3035
Reputation: 152624
I'm assuming item.CDATE_IUD
is a string representation of a date.
Linq to EF and Linq to SQL do not support string to date conversions. You can do the conversion in memory, but since you're filtering on the parsed value, then you're pulling in ALL records. You might be able to use a string comparison instead:
string today = DateTime.Today.ToString("yyyy-mm-dd"); // or whatever format your DB string is on.
var query = from item in dataContext.Items
orderby item.CDATE_IUD descending
where item.QV_USER == user
where item.QV_EXT == ext
where item.ACTIVE == 1
where item.CDATE_IUD.StartsWith(today)
select item;
Upvotes: 6
Reputation: 2227
DateTime.ParseExact(x, "yyyyMMdd",cultureInfo.InvariantCulture)
will help to convert a string in dateTime
Upvotes: -2