Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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

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

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 ...
7of9
セブンオブナインです。Unimatrix 01の第三付属物 9の7という識別番号です。Star trek Voyagerの好きなキャラクターです。まとめ記事は後日タイトルから内容がわからなくなるため、title検索で見つかるよう個々の記事にしてます。いわゆるBorg集合体の有名なセリフから「お前たち(の知識)を吸収する。抵抗は無意味だ」。Thanks in advance.
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away