ninjanye / SearchExtensions

Library of IQueryable extension methods to perform searching
MIT License
331 stars 52 forks source link

Search datetime using string #23

Open jagadeesh1492 opened 6 years ago

jagadeesh1492 commented 6 years ago

query.Search(p=>p.date).soundex("10")

search datetime

ninjanye commented 6 years ago

Could you expand on what it is you are trying to achieve?

Have you tried, query.Search(p=>p.date.ToString()).Soundex("10")

yzorg commented 6 years ago

LINQ does not support null-coalescing operator, so this is NOT possible: query.Search(p => p.ClosedDate?.ToString() ?? "");

which leads to a lot of duplicated code like this: query.Search(p => p.ClosedDate == null ? "" : p.ClosedDate.ToString());

Not asking for the feature, but after reading @ninjanye's blog posts on the topic I haven't seen a single example with NULL DATETIME columns. So more of an FYI than a feature request.

ninjanye commented 6 years ago

Hi @yzorg

Thanks for getting in touch, could you link me to the particular article you are referring to and I'll try and see if I can help clear things up

P47K0 commented 2 years ago

I also converted a DateTime to a string but how can you do a search on the full date or multiple parts of a date? I clould only manage to do a search on one part of the date, the year for example. query.Search(x => x.TransactionDateTime.ToString().Containing("2022");

Searching on "2022-03" gives an empty collection (while there are records with a TransactionDateTime of march this year). query.Search(x => x.TransactionDateTime.ToString().Containing("2022-03");

When I try to pass a format to the ToString method, I get a Linq error: query.Search(x => x.TransactionDateTime.ToString("s").Containing("2022-03")

Error: DbSet<Transaction>().Where(t => t.TransactionDateTime.ToString("s").Contains("2022-03"))' could not be translated. Additional information: Translation of method 'System.DateTime.ToString' failed. If this method can be mapped to your custom function, see https://go.microsoft.com/fwlink/?linkid=2132413 for more information.