Find news by title, case insensitive
Hi,
Im trying to search news items by title.
We already did this for documents, so I tried the same way, which is case insensitive:
var news = App.WorkWith()
.NewsItems()
.Publihed()
.Where(n => n.Title.IndexOf(myStringFilter
, StringComparison.OrdinalIgnoreCase) > -1);
var news = App.WorkWith()
.NewsItems()
.Publihed()
.Where(n => n.Title.Value.IndexOf(myStringFilter, StringComparison.OrdinalIgnoreCase) > -1);
Hello Olivier,
You can use ToLower() to find the titles. Here is a sample code:
public void findnews(string myStringFilter, string myStringFilter2)
var news = App.WorkWith()
.NewsItems()
.Publihed()
.Where(n => n.Title.ToString().ToLower() == myStringFilter2);
Hi Victor,
Thanks for your answer.
However, your example is for exact matching string comparison and I need partial matches; that's why I was trying to use IndexOf() in my question. Also, I dont know why you link the Lstring.ToLower() doc page since you are calling .ToString() before ToLower() in your example.
I made some more tests with NewsItems and I came to some results using .ToString().ToUpper().IndexOf(myFilterToUpper)
which is not very clean or optimal but at least is working.
Nevertheless there's still a problem.
In my code, the way I filter news is always the same:
news = news.Where(
n => n.Content.ToString().ToUpper().IndexOf(textFilterUpper) > -1);
IEnumerable<NewsItem> news = App.WorkWith()
.NewsItems()
.Publihed().Get().ToArray();
var news = App.WorkWith()
.NewsItems()
.Publihed().Get();
I'm also interested in an answer to this.
Are we the first people to try to develop some kind of search system with Sitefinity 4?
Hello Thomas,
Using IEnumerable
is ok, however it is good practice to construct the query at once. Also why do you cast it to Array? It will be better to apply Contains(), if you do not want to get exact match. For example:
news = news.Where(
n => n.Content.ToString().ToLower().Contains("testsearchkey")).Get();
Victor,
Maybe I was unclear.
Here's a quick example to show my problem.
var news1 = App.WorkWith()
.NewsItems()
.Publihed()
.Where(n => n.Title.ToString().ToUpper().Contains(
"EURO"
))
.Get();
var news2 = App.WorkWith()
.NewsItems()
.Publihed()
.Get()
.ToArray()
//To make sure data was fetched locally!
.Where(n => n.Title.ToString().ToUpper().Contains(
"EURO"
));
int
n1 = news1.Count();
int
n2 = news2.Count();
Hi Olivier,
We have tested the code on our end. Both of the variables ( n1 & n2 ) contained the same number of news items at the end. Please consult the attached video.
Best wishes,
Victor Velev
the Telerik team