Find news by title, case insensitive

Posted by Community Admin on 04-Aug-2018 16:28

Find news by title, case insensitive

All Replies

Posted by Community Admin on 27-Oct-2011 00:00

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);

Sadly, I get the following error at runtime with this piece of code:
Method 'IndexOf' is not supported on the 'Telerik.Sitefinity.Model.Lstring' type.

So I tried to use the Value of the Lstring:

var news = App.WorkWith()
 .NewsItems()
 .Publihed()
 .Where(n => n.Title.Value.IndexOf(myStringFilter, StringComparison.OrdinalIgnoreCase) > -1);

But now, I have this error, at runtime again, which seems to be the opposite of the first one:
Property 'System.String Value' is not defined for type 'System.String'

Both pieces of code compiled...

Can you please enlighten me on this matter ?

Thank you.

Olivier

Posted by Community Admin on 01-Nov-2011 00:00

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);
 
     

I have also researched for some articles which might be helpful to you:

http://msdn.microsoft.com/en-us/library/aa904311%28v=vs.71%29.aspx

http://www.sitefinity.com/40/help/developers-guide/overload_telerik_sitefinity_model_lstring_tolower.html

Greetings,
Victor Velev
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 03-Nov-2011 00:00

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);

But I dont have the same number of results depnding on the way I fetch my initial data:

I get more results when I get my initial data with:
IEnumerable<NewsItem> news = App.WorkWith()
                       .NewsItems()
                       .Publihed().Get().ToArray();

Than with:
var news = App.WorkWith()
                .NewsItems()
                .Publihed().Get();

Even when I remove the ToUpper() I still get different number of results with the different sources...

Can you explain me what is implemented differently in your fluent API or on database side ?

Maybe it's a bug ?

I wasted enough time on this issue.

Thank you.

Posted by Community Admin on 07-Nov-2011 00:00

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?

Posted by Community Admin on 08-Nov-2011 00:00

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();



All the best,
Victor Velev
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 08-Nov-2011 00:00

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();

Do you agree that n1 should be equal to n2 at the end of this ?

The problem I was referring to in my previous post is that they are NOT.

For my site, n2 is greater than n1. The string comparison seems to work better "locally".

Can you reproduce this or just explain it ?

If it is a bug, please report it to the dev team and PITS.

Thank you.

Olivier

Posted by Community Admin on 11-Nov-2011 00:00

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

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed