0

I want to use like type operator in linq to sql, i used contains method to achieve it, but it is case sensitive, suggest me something.

The contains method is case sensitive but I want my query to be case insensitive . I tried to use IndexOf() method also but it is giving exception.

public static List<string> GetDecodedStaticMsg(string filterMsg)
 {
     try
     {
         var q1 = (from decodemsg in Em.StaticMessages
                   where decodemsg.Message.Contains(filterMsg)
                   select
                   decodemsg.MessageId
                   ).ToList();

                  return q1;

     }
     catch (Exception)
     {
                return null;
     }
 }
jarlh
  • 42,561
  • 8
  • 45
  • 63

2 Answers2

1

You can use the CompareInfo member from the CultureInfo class, try this :

public static List<string> GetDecodedStaticMsg(string filterMsg)
{
     try
     {
         var q1 = (from decodemsg in Em.StaticMessages
                   where culture.CompareInfo.IndexOf(decodemsg.Message, filterMsg, CompareOptions.IgnoreCase) >= 0
                   select
                   decodemsg.MessageId
                   ).ToList();

                  return q1;

     }
     catch (Exception)
     {
                return null;
     }
}

Reference : https://stackoverflow.com/a/15464440/3759822

Fourat
  • 2,366
  • 4
  • 38
  • 53
0

We could use SqlMethods.Like for doing case insensitive search

public static List<string> GetDecodedStaticMsg(string filterMsg)
 {
     try
     {
         var q1 = (from decodemsg in Em.StaticMessages
                   where SqlMethods.Like(decodemsg.Message, $"%{filterMsg}%")  //Changes here
                   select
                   decodemsg.MessageId
                   ).ToList();

                  return q1;

     }
     catch (Exception)
     {
                return null;
     }
 }