Reputation: 37
I'm implementing a simple search. Right now, I can search through the posts using the terms in string array Split but I want to search the date aswell. So, I added an array of datetime. The problem I'm having is I only want to search the Date part of datetime, and ignore Time. Whenever I search, I get an error at d.Date.
IEnumerable<Post> posts = from p in post_repository.Posts
from s in split
from d in Date
where (p.Title.Contains(s) || p.ContactPhone.Contains(s) || p.Content.Contains(s) || p.Author.Contains(s) || p.ContactEmail.Contains(s)|| **p.EndDate.Date.Equals(d.Date)**) && p.Deleted == false && p.Publish == true
select p;
Error Message:
The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NotSupportedException: The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
I've been stuck at this for a while, can anyone figure out what I'm doing wrong? All the dates are initialized, so there is nothing null and everything other than date works. If I remove d.Date or EndDate.Date, there is no error but I dont get the desired result. Thanks in Advance.
Edit 1:
string[] split = query.Split(breakpoints);
DateTime[] arrayOfDateTimes = Datify(split);
The Declaration might seem really stupid. Datify is a function I wrote which returns an array of DateTime which works properly cause I went through it step by step looking at the local variables.
private readonly char[] breakpoints = {'.', ',', ' ', ':', '\t' };
Query is the search, its a string.
Upvotes: 0
Views: 789
Reputation: 4908
Use the class EntityFunction for trimming the time portion.
using System.Data.Objects;
EntityFunctions.TruncateTime(EndDate)
Upvotes: 1