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

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

http://qiita.com/7of9/items/a5e6b032d16f4042f1f4#try3-dictionarystring-liststring

## code v0.1

[@ C# Online Snippet Compiler](http://www.volatileread.com/utilitylibrary/snippetcompiler?id=72671)

```csharp
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");
  
  }
}
```

```txt:結果
---
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](http://www.volatileread.com/utilitylibrary/snippetcompiler?id=72672)

```csharp
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");
  
  }
}
```

```txt:結果
---
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 ...
```