Ren
Ren

Reputation: 13

How can I Using LinQ search in a list by the input and print those that contain the letters?

Hi Im new to Linq and been trying to do this query but wont work, I have problems with the printing of the names, for example it does ask me the letters (inicials) I want, but when it comes to showing the matches they dont print

Here is the query:

        string input;
        Console.Write("Which fist 2 letters you want to look for? \n");
        input = Console.ReadLine();

        var search= from s in names
                     where s == input 
                     orderby s descending
                     select s;

        foreach (var name in search) 
        {
            Console.Write("\t" + name); 
            
        }
         

Here is my list:

string[] names= { "ANE", "CARL", "ANNA", "SOFIE", "ANNIE", "MARIE", "CAMILA", "SCOTT", "STILES", "JESSICA" };

The program does ask for the input but won't print anything as if there are no matches even when there are. How can I make results show up?

example of output OUTPUT

Upvotes: 0

Views: 84

Answers (1)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186678

Are you looking for a StartsWith?

var search = names
  .Where(name => name.StartsWith(input))
  .OrderBy(name => name);

Upvotes: 3

Related Questions