-1

オブジェクトのリストを検索するためのテキスト ボックスがある Web アプリケーションを実行しています。

オブジェクトは次のようになります。

    public class Project : IDbProject
    {
      public string ProjectName { get; set; }
      public string Country { get; set; }
      public string Customer { get; set; }
      public DateTime UploadDate { get; set; }
      public DateTime CreateDate { get; set; }
      public string CreatedBy { get; set; }
    }

、リストはIList<IProject>. 私の検索/フィルタリングはリストを通過し、次のようにオブジェクト内の各文字列と比較します:

        public IList<IProject> GetSearchedProjects(string searchString)
        {

       foreach (var proj in _projects)
                {

                    if (InputStartWithSearch(proj.ProjectName, searchString) && !searchProjects.Contains(proj))
                        {
                            searchProjects.Add(proj);
                            continue;
                        }
                    if (InputStartWithSearch(proj.Country, searchString) && !searchProjects.Contains(proj))
                        {
                            searchProjects.Add(proj);
                            continue;
                        }
                    if (InputStartWithSearch(proj.CreatedBy, searchString) && !searchProjects.Contains(proj))
                        {
                            searchProjects.Add(proj);
                            continue;
                        }
                    if (InputStartWithSearch(proj.ProjectState, searchString) && !searchProjects.Contains(proj))
                        {
                            searchProjects.Add(proj);
                            continue;
                        }
                    if (IsStringDate(searchString))
                        if (IsDatesEqual(proj.CreateDate, searchString) && !searchProjects.Contains(proj))
                        {
                            searchProjects.Add(proj);
                        }
                    }
            return searchProjects;

        }
        return _projects;
    }

ご覧のとおり、オブジェクトの文字列/日付などで検索文字列をチェックする多くのプライベート メソッドを作成しました。そして、それはすべてうまく機能します。

しかし、単一の文字列でオブジェクトのリストを検索するためのより良い/より高速な方法はありますか?

編集

メソッドInputStartWithSearchIsStringDateはプライベート メソッドで、入力文字列がプロジェクトのデータで始まるかどうかを確認します。したがって、CreatedBy が「Matthi Smith Junior」で、「Matthi Junior」、「Matthi Smith」、「Smith Junior」などを検索すると、それが追加されます。

IsStringDate は、検索文字列が Datetime 形式と等しいかどうかをチェックしています。したがって、形式の配列が含まれており、検索文字列がその形式であるかどうかを確認します。

4

4 に答える 4

0

コードを少し変更して、キーがプロジェクト名で値がオブジェクトである Dictionary オブジェクトを使用できます。クラスは次のようになります。

public class Project : IDbProject
{
  public string Country { get; set; }
  public string Customer { get; set; }
  public DateTime UploadDate { get; set; }
  public DateTime CreateDate { get; set; }
  public string CreatedBy { get; set; }
}

次に、新しい辞書オブジェクトを作成します

Dictionary <string, Project> myProjects = new Dictionary<string, Project>();

次に、文字列をプロジェクト名に設定して入力し、参照する場合は次のようにします。

Project projectDetails = myProjects["projectName"];

明らかに、「projectName」を必要なものに変更してください。

編集

あなたができる開始に基づいて(コードはテストされていません)

Project projectDetails = myProjects.Where (m => m.Key.Contains ("projectName").Select (m => m.Value);
于 2013-10-24T09:01:13.877 に答える
0

まず、ループの先頭にあるガード ステートメントに searchProjects.Contains() チェックをリファクタリングできます。それは物事を単純化するでしょう。次に、各プロジェクトのプロパティを、一意の区切り文字を使用して 1 つの文字列に連結できます。その後、検索はより簡単です。これは、既存の方法よりも速くはありません。単に短いだけです。

public IList<IProject> GetSearchedProjects(string searchString)
{
    foreach (var proj in _projects)
    {
        if (!searchProjects.Contains(proj))
            continue;
        StringBuilder sb = new StringBuilder();
        sb.Append("|").Append(proj.ProjectName);
        sb.Append("|").Append(proj.Country);
        sb.Append("|").Append(proj.CreatedBy);
        sb.Append("|").Append(proj.UploadDate.ToString());

        if (sb.ToString().Contains("|" + searchString))
            searchProjects.Add(proj);
    }
    return searchProjects;
}

'|' 文字がどのフィールドにも表示されません...

于 2013-10-24T09:09:24.947 に答える
0

linq を使用できます。繰り返しますが、高速ではありませんが、コードはより単純です。

public IList<IProject> GetSearchedProjects(string searchString)
{
    return (from p in _projects 
            where searchProjects.Contains(p) &&
                  (InputStartWithSearch(p.ProjectName, searchString) || 
                  InputStartWithSearch(p.Country, searchString) ||
                  InputStartWithSearch(p.CreatedBy, searchString) || 
                  DateToString(p.CreateDate).Contains(searchString.ToLower()))
            select p).ToList();
}
于 2013-10-24T09:12:29.260 に答える