❔ Error message: Cannot convert query to sql
hi i have this code, on line 119 is whats causing the error saying the query cannot be converted to sql. But this error wont occur if I add ToList() at line 115. But the reason we dont want to put ToList() at line 115 is to not get retrieve all the data from the database because at the bottom code we are doing pagination and we only need 50 records to retrieve not all the datas. Also it makes our query slow because of putting ToList() at line 115 is there a way for us to avoid retrieving datas from the database and still execute line 119? here is the code for line 119, also the 2nd picture is what the error message is.
projectQuery = projectQuery.Where(o => DateTime.ParseExact(o.WindowEndDateString.Split(" ", StringSplitOptions.None)[0], formats, new CultureInfo("en-US"), DateTimeStyles.None) >= startDate && DateTime.ParseExact(o.WindowStartDateString.Split(" ", StringSplitOptions.None)[0], formats, new CultureInfo("en-US"), DateTimeStyles.None) <= endDate);
6 Replies
here is the code for line 119 if you cant read it with text only from above
you can use
.Skip()
and .TakeAsync()
to only take the amount of items you need (note that you need to .OrderBy()
to get reliable pagination)
it might not know how to translate the parse and split to SQLhello @tvde1, we actually have that code at the bottom
our previous code was ToList() was called on line 115 not on line 130 and everything was fine. but the performance was very slow its taking 10 - 15 seconds to load. thats why we placed the ToList() to line 130 instead of line 115 because we dont need to display all the datas but because of that line 119 is having a problem now
it cannot convert line 119 into an SQL query part. So you'd have to rewrite it in a way in LINQ, so it can turn it into SQL, or have the database query execute before that line
SQL can't parse or split
Store your dates properly in the database and there will be no need to do either
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.