0

In my application iam having a search functionality , and i implemented it like below.

placelist = placelist.Where(i => i.category_name.Contains(postData)).Union(placelist.Where(i => i.category_address.Contains(postData))).Union(placelist.Where(i => i.category_tags.Contains(postData))).ToList();

eventlist = eventlist.Where(i => i.event_name.Contains(searchconte)).Union(eventlist.Where(i=>i.event_location.Contains(searchconte))).ToList();

indexlist = indexlist.Where(i => i.restaurant_location.Contains(searchtext)).Union(indexlist.Where(i => i.restaurant_name.Contains(searchtext))).Union(indexlist.Where(i=>i.cuisine_type.Contains(searchtext))).Union(indexlist.Where(i=>i.special_dishes.Contains(searchtext))).ToList();

when iam Entering the postData as "bamb", it is returning nothing , But when i enter the same thing like "Bamb", It is showing the results.

It is searching only Case-Sensitive, I want modify this statement in such a way that it should search and return the content whether it is Case-sensitive or not, and also it should for the content with coma(,) colon(Smile | :) ,Quotes("",'')

How can i modify the statement to get the above mentioned functionality.

userhi
  • 553
  • 1
  • 7
  • 27

3 Answers3

3

You don't need all these unions, you can use OR logical operator ( || ):

var results = (from item in indexlist
                where ContainsIgnoreCase(item.restaurant_location, searchText)
                    || ContainsIgnoreCase(item.restaurant_name, searchText)
                    || ContainsIgnoreCase(item.cuisine_type, searchText)
                    || ContainsIgnoreCase(item.special_dishes, searchText)
                select item).ToList();

private static bool ContainsIgnoreCase(string str, string search)
{
    return str.IndexOf(search, StringComparison.InvariantCultureIgnoreCase) >= 0;
}

String.IndexOf can be used as an alternative to Contains + Ignore Case.

As a plus:

You can extend the string class with this functionality by creating an extension method:

public static class MyExtensions
{
    public static bool Contains(this string str, string value,  
                                                        StringComparison comparisonType)
    {
        return str.IndexOf(value, comparisonType) >= 0;
    }
}

Usage:

string str = "34bamb12";
str.Contains("Bamb", StringComparison.InvariantCultureIgnoreCase); // true
Zein Makki
  • 29,485
  • 6
  • 52
  • 63
0

The easiest way to call .ToUpper() on every string or .ToUpperInvariant() suit better in your case I believe.

0

You can use toUpper method like this:

eventlist = eventlist.Where(i => i.event_name.ToString().ToUpper().Contains(searchconte.ToUpper())).Union(eventlist.Where(i=>i.event_location.ToString().ToUpper().Contains(searchconte.ToUpper()))).ToList();
Curious
  • 474
  • 1
  • 8
  • 25