-2

I´ve done a school homework where we need to be able to search in our program (console app).

I´ve created an object (Sodacrate) which contains an array of objects (sodabottles) which have an index length of 24. The sodabottles have name, price and type variables We can not use LISTS for this according to our teacher (probably because he want´s us to find other ways to solve this), that´s the reason for an array containing objects.

When I´m searching I can search for the exact name of the sodabottle, or I can search for the first letter. If I for example have a bottle of Coca Cola (exact spelling) and I´m searching for coca, then I won´t find a match. If I search for c, C or Coca or even Coca Cola I´ll find a match.

How do I make the searchresult convert ToLower? I´ve tried this and also searched the problem, but didn´t find any clear solutions.

It gives me an compiling error

Error CS0029 Cannot implicitly convert type 'string' to 'System.Collections.Generic.IEnumerable' thesodacrate C:\Users\Benny\source\repos\thesodacrate\thesodacrate\Sodacrate.cs 244 Active

I´m sorry if this seems like simple thing, but I´m almost getting bald over this. I also need to say that this is a whole new territory for me, and I´m mostly doing trial and error to figure this out.

I´ve edited the code so it´s up to date how it looks atm. entries = entries on row 7 creates the compiling error.

 Console.Write("Type in your search: ");
            var keyword = Console.ReadLine();
            keyword = keyword.ToLower();
            Console.WriteLine(keyword);
            var entries = bottles.Where(entry => entry.SodaBottleName !=null && entry.SodaBottleName.Contains(keyword));
            entries = entries.ToString().ToLower();

            if (entries.Count() == 0)
            {
                Console.Write("Didn´t find any match");
                Console.WriteLine("Press enter to return to mainmenu");
            }
            else
            {

                foreach (var entry in entries)
                {
                    Console.WriteLine("{0} - {1} $", entry.SodaBottleName, entry.SodaBottlePrice);
                }
                Console.WriteLine("You´ve added {0} bottles that match your search: {1}.", entries.Count(), keyword);
                Console.WriteLine("Press enter to return to mainmenu");
            }
            Console.ReadLine();
  • https://learn.microsoft.com/en-us/dotnet/api/system.string.equals?view=netframework-4.7.2#System_String_Equals_System_String_System_String_System_StringComparison_ – aybe Mar 31 '19 at 22:40
  • [Msdn Docs - How to compare strings in C#](https://learn.microsoft.com/en-us/dotnet/csharp/how-to/compare-strings). – Erik Philips Mar 31 '19 at 22:45
  • 1
    strings are immutable. This means that _keyword.ToLower()_ doesn't change _keyword_ but returns a new string with the result of ToLower(). If you want keyword to be in lowercase you need to write _keyword = keyword.ToLower()_ – Steve Mar 31 '19 at 22:49
  • Sorry guys. I don´t think I can use the Equals because I need to be able to type in fragments of the word. And from what I understand the Equals compare two strings to each other and returns true or false? If i type in coca in the search field and I would like the searchmethod to return all index which contains the name Coca Cola, the Equals would return false? – Potatismoose Mar 31 '19 at 22:53
  • Thanks Steve for bringing that to clarity. I´ve changed that in my code now, but if I do the same on the searchresult it gives me an compiling error. Severity Code Description Project File Line Suppression State Error CS0029 Cannot implicitly convert type 'string' to 'System.Collections.Generic.IEnumerable' thesodacrate C:\Users\Benny\source\repos\thesodacrate\thesodacrate\Sodacrate.cs 243 Active – Potatismoose Mar 31 '19 at 22:57
  • If you changed something and are now getting an error, you need to [edit your question](https://stackoverflow.com/posts/55446099/edit) to show us what you changed and what error you are getting. – Dour High Arch Mar 31 '19 at 23:06

1 Answers1

0

You need to make the both strings to lower. The Keyword and the string.

var keyword = Console.ReadLine().ToLower();

and

entry.SodaBottleName.ToLower().Contains(keyword)
  • However, that’s not how strings should be compared in C#. Here it should be `entry.SodaBottleName.IndexOf(keyword, StringComparison.CurrentCultureIgnoreCase) >= 0` in order to avoid unnecessary copy operations and memory allocations which would happen with calling `ToLower`. – ckuri Mar 31 '19 at 23:01