LoginSignup
1
2

More than 5 years have passed since last update.

csharp > 部分一致検索 > Dictionary <string, List <string>>()

Last updated at Posted at 2016-08-20

名前の部分一致検索が在庫管理システムの機能として欲しい。
それに関して、以下で使っていたDictonaryとListの組合せを試した。

code v0.1

@ C# Online Snippet Compiler

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
  public static void displayAllElementWithKey(ref Dictionary<string, List<string>> myDic, string searchKey) {
        foreach(KeyValuePair<string, List<string>> pair in myDic) {
        if (pair.Key.Contains(searchKey) == false) {
                continue;
            }
            Console.WriteLine("key:" + pair.Key.ToString()); // Debug.Log() for Unity
            foreach(var element in pair.Value) {
               Console.WriteLine("about:" + element); // Debug.Log() for Unity
            }
        }    
  }

  public static void Main()
  {
    Dictionary <string, List<string>> myDic 
      = new Dictionary<string, List<string>> ();
    string keystr;

    // data 1
    keystr = "MAX232";
    var infols1 = new List<string>();
    infols1.Add("The MAX220-MAX249 family of line drivers/receivers is ...");
    myDic.Add(keystr, infols1);

    // data 2
    keystr = "MAX44242";
    var infols2 = new List<string>();
    infols2.Add("The MAX44242 provides a combination of high voltage, low noise, low input bias current ...");
    myDic.Add(keystr, infols2);


    Console.WriteLine("---");
    displayAllElementWithKey(ref myDic, /* searchKey=*/ "MAX232");
    Console.WriteLine("---");
    displayAllElementWithKey(ref myDic, /* searchKey=*/ "MAX44242");
    Console.WriteLine("---");
    displayAllElementWithKey(ref myDic, /* searchKey=*/ "MAX");

  }
}
結果
---
key:MAX232
about:The MAX220-MAX249 family of line drivers/receivers is ...
---
key:MAX44242
about:The MAX44242 provides a combination of high voltage, low noise, low input bias current ...
---
key:MAX232
about:The MAX220-MAX249 family of line drivers/receivers is ...
key:MAX44242
about:The MAX44242 provides a combination of high voltage, low noise, low input bias current ...

一応動いているが、3000件などになった時に遅いだろうか。

code v0.2

keyで取得される情報を複数にした (Case No, Column, about)。

@ C# Online Snippet Compiler

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
  public static void displayAllElementWithKey(ref Dictionary<string, List<string>> myDic, string searchKey) {
        foreach(KeyValuePair<string, List<string>> pair in myDic) {
              if (pair.Key.Contains(searchKey) == false) {
                continue;
            }
            Console.WriteLine("key:" + pair.Key.ToString()); // Debug.Log() for Unity
            foreach(var element in pair.Value) {
                    Console.WriteLine(element); // Debug.Log() for Unity
            }
        }    
  }

  public static void Main()
  {
    Dictionary <string, List<string>> myDic 
      = new Dictionary<string, List<string>> ();
    string keystr;

    // data 1
    keystr = "MAX232";
    var infols1 = new List<string>();
    infols1.Add("Case No:1");
    infols1.Add("Column:1");
    infols1.Add("about:The MAX220-MAX249 family of line drivers/receivers is ...");
    myDic.Add(keystr, infols1);

    // data 2
    keystr = "MAX44242";
    var infols2 = new List<string>();
    infols2.Add("Case No:1");
    infols2.Add("Column:2");
    infols2.Add("The MAX44242 provides a combination of high voltage, low noise, low input bias current ...");
    myDic.Add(keystr, infols2);


    Console.WriteLine("---");
    displayAllElementWithKey(ref myDic, /* searchKey=*/ "MAX232");
    Console.WriteLine("---");
    displayAllElementWithKey(ref myDic, /* searchKey=*/ "MAX44242");
    Console.WriteLine("---");
    displayAllElementWithKey(ref myDic, /* searchKey=*/ "MAX");

  }
}
結果
---
key:MAX232
Case No:1
Column:1
about:The MAX220-MAX249 family of line drivers/receivers is ...
---
key:MAX44242
Case No:1
Column:2
The MAX44242 provides a combination of high voltage, low noise, low input bias current ...
---
key:MAX232
Case No:1
Column:1
about:The MAX220-MAX249 family of line drivers/receivers is ...
key:MAX44242
Case No:1
Column:2
The MAX44242 provides a combination of high voltage, low noise, low input bias current ...
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2