簡體   English   中英

搜索列表C#

[英]Searching a list C#

希望一切都很好。

我在這里遇到一個簡單的問題。 我正在嘗試創建一個控制台應用程序,允許您搜索列表中的單詞(通過一個或多個字符,即u =用戶,用戶組)。

我似乎無法通過此錯誤:錯誤CS0305使用泛型類型'List'需要1個類型參數wordSearch c:\\ Projects \\ wordSearch \\ wordSearch \\ Program.cs 36 Active

請在下面找到我的代碼.....歡迎任何幫助提前謝謝。


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();
        }
    }
}

}

您的代碼有兩個問題:

  1. 你必須保存用戶的輸入(“......用戶正在尋找的第一個字母......”),例如你已經用作過濾器的mySearchString
  2. 您必須查詢和掃描items 實例 ,而不是System.Collections.Generic.ListList 類型

你可以這樣說:

  ...

  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();
  }

  ...

編輯 :似乎實際的請求是循環查詢列表, foreach不復制粘貼,如下所示:

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();
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM