Mohammed Gani
Mohammed Gani

Reputation: 11

Searching a list C#

Hope all is well.

I am struggling with a simple problem here. I am trying to create a console app that allows you to search for words in a list (by one or more characters ie u = user, user group).

I can't seem to get past this error: Error CS0305 Using the generic type 'List' requires 1 type arguments wordSearch c:\Projects\wordSearch\wordSearch\Program.cs 36 Active

Please find my code below..... Any help is welcome Thank you in advance.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace wordSearch
{


public class MyList<T> { }
public class TestList<T> { }

class MyList
{
    public static void Main()
    {

        string searchKeyword = "o";

        List<string> items = new List<string>();
        items.Add("User");
        items.Add("User Groups");
        items.Add("User Activity Log");
        items.Add("Report Designer");
        items.Add("Report Activity Log");



        List<string> searchResults = items.FindAll(u => 
         u.Contains(searchKeyword));


            Console.WriteLine("Please type in the first letter of item you 
            are looking for:");
            Console.ReadLine();

        foreach (var result in System.Collections.Generic.List.Where(u => 
         u.IndexOf(mySearchString) == 0))
        {
            Console.WriteLine("User, User Groups, User Activity Log");
            Console.ReadKey();
        }
       foreach (var result in List.Where(r => r.IndexOf(mySearchString) == 
        0))
        {
            Console.WriteLine("Report Desinger, Report Activity Log");
            Console.ReadKey();
        }
    }
}

}

Upvotes: 1

Views: 1302

Answers (1)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186813

There are two issues with your code:

  1. You have to save user's input ("...the first letter user is looking for..."), e.g. in mySearchString which you've already used as filter.
  2. You have to query and scan items instance, not System.Collections.Generic.ListList type:

You can put it like this:

  ...

  Console.WriteLine("Please type in the first letter of item you are looking for:");

  //DONE: user input saved
  string mySearchString = Console.ReadLine();

  //DONE: we scan items, not List 
  foreach (var result in items.Where(u => u.IndexOf(mySearchString) == 0)) {
      Console.WriteLine("User, User Groups, User Activity Log");
      Console.ReadKey();
  }

  // DONE: we scan items not List
  foreach (var result in items.Where(r => r.IndexOf(mySearchString) == 0)) {
    Console.WriteLine("Report Desinger, Report Activity Log");
    Console.ReadKey();
  }

  ...

Edit: It seems that the actual request is to query list in a loop, foreach not copy-pasting, something like this:

public static void Main() {
  List<string> items = new List<string>() {
    "User",
    "User Groups",
    "User Activity Log",
    "Report Designer",
    "Report Activity Log",
  }

  while (true) {
    Console.WriteLine("Please type in the first letter of item you are looking for:");
    Console.WriteLine("Prease press enter (i.e. type an empty string) to quit"); 

    string mySearchString = Console.ReadLine();

    if (string.IsNullOrEmpty(mySearchString)) 
      break;

    foreach (var item in items.Where(r => r.IndexOf(mySearchString) == 0))
      Console.WriteLine(item);

    Console.WriteLine();
  }
}

Upvotes: 3

Related Questions