I do need to compare all the fields as (partial) strings since the user is entering a
search string (in a column header): so if a date is "10/11/12" and the user enters "/12" then that row should return to the grid. Am I incorrect in thinking to covert the Dates to strings?
here's the whole block of code with the broken "Date" toString()'s (guess I should of posted it earlier):
var Articles = (from
articles in
newsDB.Articles
where
articles.Id.ToString().Contains(SearchArticleCriteria.Id.ToString()) ||
TacRedline
Member
177 Points
300 Posts
Easy question? how do I code a *WHERE X LIKE Y
Dec 12, 2012 02:09 PM|LINK
Here's what I have, I can compare the two objects with a "WHERE X == Y" no problem but how do I compare object properties with a "WHERE X LIKE Y"?
Any links showing the how to do this would be great too, thanks!
Thanks!
NewsEntities newsDB = new NewsEntities(); //DBContext
var Articles = (from articles in newsDB.Articles
where articles.Title LIKE SearchArticleCriteria.Title //<--- how to code that?
select new { articles.Id, articles.Title, articles.CategoryId, articles.DateNews, articles.DateLastPublished, articles.DateLastEdit }).ToList();
Dr. Acula
Participant
1441 Points
319 Posts
Re: Easy question? how do I code a *WHERE X LIKE Y
Dec 12, 2012 02:11 PM|LINK
you can use .Contains
TacRedline
Member
177 Points
300 Posts
Re: Easy question? how do I code a *WHERE X LIKE Y
Dec 12, 2012 02:22 PM|LINK
Any for dates and other fields use ToString() ? e.g.
articles.DateNews.ToString().Contains(SearchArticleCriteria.DateNews.ToString())
nope that didn't work, throws:
LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.
me_ritz
Star
9337 Points
1447 Posts
Re: Easy question? how do I code a *WHERE X LIKE Y
Dec 12, 2012 02:33 PM|LINK
Contains is the way to go for LIKE in SQL.
But date doesn't work like that. Check out these links -
http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/c4083f8a-4be3-46a9-ab10-be26519a5809
http://forum.linqpad.net/discussion/58/comparing-dates-in-linqpad
TacRedline
Member
177 Points
300 Posts
Re: Easy question? how do I code a *WHERE X LIKE Y
Dec 12, 2012 03:03 PM|LINK
Thanks guys,
I do need to compare all the fields as (partial) strings since the user is entering a search string (in a column header): so if a date is "10/11/12" and the user enters "/12" then that row should return to the grid. Am I incorrect in thinking to covert the Dates to strings?
here's the whole block of code with the broken "Date" toString()'s (guess I should of posted it earlier):
var Articles = (from articles in newsDB.Articles
where articles.Id.ToString().Contains(SearchArticleCriteria.Id.ToString()) ||
articles.Title.Contains(SearchArticleCriteria.Title) ||
articles.CategoryId.ToString().Contains(SearchArticleCriteria.CategoryId.ToString()) ||
articles.DateNews.ToString().Contains(SearchArticleCriteria.DateNews.ToString()) ||
articles.DateLastPublished.ToString().Contains(SearchArticleCriteria.DateLastPublished.ToString()) ||
articles.DateLastEdit.ToString().Contains(SearchArticleCriteria.DateLastEdit.ToString())
select new { articles.Id, articles.Title, articles.CategoryId, articles.DateNews, articles.DateLastPublished, articles.DateLastEdit }).ToList();
Dr. Acula
Participant
1441 Points
319 Posts
Re: Easy question? how do I code a *WHERE X LIKE Y
Dec 12, 2012 03:21 PM|LINK
It depends how your user is entering the search terms. When a user enters "/12" do you know that it is the year they are searching for?
using .Contains("/12") on strings you could return "01/01/12" or "01/12/09".
If you know they are specifically looking for the year you can do yourDateTime.Year == 12 instead
TacRedline
Member
177 Points
300 Posts
Re: Easy question? how do I code a *WHERE X LIKE Y
Dec 12, 2012 03:53 PM|LINK
no, it just searches as a string: so "/12" would return "true" for any month or year "/12". With that, they should be strings?
pratikkapadi...
Member
172 Points
32 Posts
Re: Easy question? how do I code a *WHERE X LIKE Y
Dec 12, 2012 04:15 PM|LINK
Try this.TacRedline
Member
177 Points
300 Posts
Re: Easy question? how do I code a *WHERE X LIKE Y
Dec 12, 2012 06:05 PM|LINK
Thanks Pratikkapadia:
I get a compile-time error: Cannot convert lambda expression to type 'bool' because it is not a delegate type
CODE:
var Articles = (from articles in newsDB.Articles
where(p=>p.articles.Title.Contains(SearchArticleCriteria.Title))
select new { articles.Id, articles.Title, articles.CategoryId, articles.DateNews, articles.DateLastPublished, articles.DateLastEdit }).ToList();
I googled the error but I'm not sure of the answers:
http://stackoverflow.com/questions/11366218/cannot-convert-lambda-expression-when-comparing-2-integers
http://stackoverflow.com/questions/8544768/assigning-values-via-a-lambda-expressions
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Easy question? how do I code a *WHERE X LIKE Y
Dec 13, 2012 04:28 AM|LINK
Hi,
In fact if you wanna check whether any of the article's Title meets what you need, please use "Any" method instead:
var Articles = (from articles in newsDB.Articles.AsEnumerable() where(p=>p.articles.Any(q=>q.Title.ToString()==SearchArticleCriteria.Title.ToString())) select new { articles.Id, articles.Title, articles.CategoryId, articles.DateNews, articles.DateLastPublished, articles.DateLastEdit }).ToList();