2013-05-06 10 views
5

私はxmlファイルを照会して選択ごとに3つの属性を返します(条件を満たす各エントリは3つの属性の詳細を返します)。私はこれらの値を格納し、その後最初の属性を検索し、それに関連する他の2つの格納された属性を返す必要があります。複数のxml属性に基づいてデータを格納および検索する方法は?

var items = from item in doc.Descendants("SUM") 
         select new 
         {          
          id = (string)item.Attribute("id"), 
          category = (string)item.Attribute("cat"), 
          selection = (string)item.Attribute("sel") 
         }; 

上記のコードが見つかりました。項目あたり3つの属性を返します。これらの3つのエントリを関連付けるようにこれらの3つのエントリを格納し、その後、格納されたエントリの参照を後で実行する必要があります。たとえば、id = 1の格納された値を参照して、対応するカテゴリと選択項目を返すことができるようにする必要があります。

私はC#のLookupメソッドを研究していましたが、それを使用する方法を理解していません。リストはうまくいくようですが、リスト内のエントリに複数のデータを格納する方法はわかりません(1つのエントリに連結することもできますが、その上でルックアップを実行するかどうかはわかりません)。 LISTまたはLOOKUP(または他の言及されていない方法)でこれを行う方法に関する提案は高く評価されます。

答えて

3

あなたがこれをさらにフィルタリングするWhere(例えばFirstOrDefaultなどとして、または他のオプションを)使用することができます。

var items = from item in doc.Descendants("SUM") 
        select new 
        {          
         Id = (string)item.Attribute("id"), 
         Category = (string)item.Attribute("cat"), 
         Selection = (string)item.Attribute("sel") 
        }; 

var filtered = items.Where(i => i.Id == 1); 

// Then use these as needed 
foreach(var item in filtered) 
{ 
    Console.WriteLine("Cat: {0}, Sel: {1}", item.Category, item.Selection); 
} 

ToLookup方法は実際には非常に異なる目的を果たします。 ILookup<T,U>を実装するデータ構造を構築します。ルックアップテーブルは、特定のキーに一致するすべての項目を簡単に返すことができます。これは、データから多数のルックアップを実行する場合に便利ですが、単一の値に一致するアイテムを単に検索する場合は便利です。あなたはDictionary<string, Item>でコレクションを保存でき、インデクサプロパティでルックアップのために、あなたのルックアップはIDによって常にをしている場合、

public class Item // come up with a better name... 
{ 
    public string ID {get; set;} 
    public string Catagory {get; set;} 
    public string Selection {get; set;} 
} 

第二:

2

最初のステップは、データを格納するクラスを作成することです:

// add 
var dict = (from item in doc.Descendants("SUM") 
      select new Item 
      {          
       ID = (string)item.Attribute("id"), 
       Category = (string)item.Attribute("cat"), 
       Selection = (string)item.Attribute("sel") 
      }) 
      .ToDictionary(i=>i.ID, i=>i); 
// lookup 
Item foundItem = dict[lookupID]; 

あなたの検索がより一般的である必要があり、その後ちょうどList<Item>に保存し、LINQのとラムダ関数で検索を行う場合

List<Item> myList = new List<Item>(); 

// add items 
List.Add(item); 

// lookup one 
Item item = myList.Single(i => i.ID == lookupID); 
// lookup many 
var items = myList.Where(i => i.Category == lookupCategory); 
0

さて、あなたはおそらくに選択する実数型のでしょう:

public class Item 
{          
    public string id { get; set; } 
    public string category { get; set; } 
    public string selection { get; set; } 
}; 

を次に、あなたは

IEnumberable<Item> items = from item in doc.Descendants("SUM") 
        select new 
        {          
         id = (string)item.Attribute("id"), 
         category = (string)item.Attribute("cat"), 
         selection = (string)item.Attribute("sel") 
        }; 

Item itemIWant = items.Where(item => item.id == "someIdNumber") 
         .FirstOrDefault(); 
if (itemIWant != null) 
{ 
    // do stuff with itemIWant.category and itemIWant.selection 
} 

または複数の一致

がある場合のようないくつかのことを行うことができます
IEnumberable<Item> itemsICareAbout = 
     items.Where(item => item.id == "someIdNumber'); 
foreach(Item item in itemsICareAbout) 
{ 
    // do stuff for each item 
} 
関連する問題