4

I'm trying to populate all the results that contains the letters provided by the user.

foreach (var item in entries.PhonebookList.Where(ab => ab.FirstName.Contains(fName, StringComparison.InvariantCultureIgnoreCase)))
{
    Console.WriteLine("Record: {0}  {1}  {2} ", item.ItemNumber, item.FirstName, item.LastName);
}

But I'm getting an error inside the Contains method.

"No overload for method 'Contains' takes 2 arguments"

What is the best way to use Contains with StringComparison.InvariantCultureIgnoreCase method?

Thank you.

Green Falcon
  • 818
  • 3
  • 17
  • 48
anonymousRey
  • 129
  • 3
  • 12

2 Answers2

2

According to the documentation here you should use string.IndexOf(string, StringComparison) instead.

So basically its:

    var entries = new 
    {
      PhonebookList = new[]
      {
        new { ItemNumber = 1, FirstName = "Leonard", LastName = "Hofstadter" }, 
        new { ItemNumber = 2, FirstName = "Sheldon", LastName = "Cooper" }, 
        new { ItemNumber = 3, FirstName = "Howard", LastName = "Wolowitz" }, 
        new { ItemNumber = 4, FirstName = "Sheng", LastName = "?" }, 
      }
    };
    
    var fName = "she";
    foreach (var item in entries.PhonebookList.Where(ab => ab.FirstName.IndexOf(fName, StringComparison.InvariantCultureIgnoreCase) >= 0))
    {
        Console.WriteLine("Record: {0} {1} {2}", item.ItemNumber, item.FirstName, item.LastName);
    }

Which returns:

Record: 2 Sheldon Cooper

Record: 4 Sheng ?

If you often need a Contains with a different StringComparison the documentation also provides an extension method you can implement, to use it like Contains (see the given example in the provided link).

Community
  • 1
  • 1
ckuri
  • 3,784
  • 2
  • 15
  • 17
  • I didn't get any error. I got the same result. if I use "she" as parameter for fName variable. It didnt display the expected result. I have Sheng and Sheldon in my for firstname. – anonymousRey Aug 13 '17 at 12:17
  • See my updated example. I suppose you should double-check if your FirstName fields really contain the names you think they contain. – ckuri Aug 13 '17 at 12:33
  • Yup I did. This is what I've observed. I have the following firstnames Breth Dioren Klarence Rey If the parameter of my fName is "re". These names will display. However if i use "RE". They wont be displayed. – anonymousRey Aug 13 '17 at 12:34
  • I checked it with your new examples. And it works for me regardless of the search terms casing. What is the type of PhonebookList? Is it from an EntityFramework / NHibernate /ORM mapper query, i.e. of type IQueryable<...>? If yes, you may need to alter your DB table schema (see https://stackoverflow.com/questions/14400856/entity-framework-case-insensitive-contains). – ckuri Aug 13 '17 at 12:39
  • public List PhonebookList { get; set; } – anonymousRey Aug 13 '17 at 12:47
  • Then it should work. Did you check that your search text doesn't contain whitespace? E.g. if your fName is "RE" is fName.Length == 2? – ckuri Aug 13 '17 at 12:56
  • @anonymousRey Apparently this is a correct answer in the context of LINQ to Objects and general C#. If you claim it doesn't work (while it should), consider providing [mcve] to prove your claims, otherwise I would consider them being incorrect. – Ivan Stoev Aug 13 '17 at 12:57
  • Sorry Ckuri. my bad on my side. there was a variable before my foreach that handles if the parameter being provided has corresponding result on my list. I modified it and its now working. Your answer now works for me. Thank you. – anonymousRey Aug 13 '17 at 12:58
0

You should use String.IndexOf method instead of Contains. If you are interested in the position of the sub string value in the current instance, you can call the IndexOf method to get the starting position of its first occurrence, or you can call the LastIndexOf method to get the starting position of its last occurrence. IndexOf(String) method is used if a sub string is found in a string instance. Also you can use other overloads of String.IndexOf based on your purpose like the one with String and StringComparison arguments.

Green Falcon
  • 818
  • 3
  • 17
  • 48