Veröffentlicht 28. April 200916 j Hi! Ich habe folgendes Problem: Ich erstelle ein Struct: public struct CommandStruct { public string aCategory; // Eine Category kann mehrere Commands haben. public string aCommand; // Ein Command kann nur eine Category haben. (Jedes Command ist eingigartig) public string aShortcut; // Ein Shortcut kann nur zu einem Command gehören. public string aToolTip; // Mehrere Commands können einen gleichen ToolTip haben. public image aImage; // Mehrere Commands können sich ein Image teilen. } Nun mache ich mir eine Liste daraus: List<CommandStruct> m_CommandStructList = new List<CommandStruct>(); Ich möchte nun zum Beispiel folgendes wissen: Ich habe eine Category und das dazugehörige Command. Nun möchte ich den ShortCut dazu wissen. Anderes Beispiel: Ich habe eine Category und möchte die jeweiligen Commands dazu wissen. Kann ich das mit der Liste machen? Wenn nicht: Gibt es einen anderen (besseren) Weg? Habe es auch mal mit mehreren Dictionarys probiert. Das hat aber den Nachteil das man immer nur einen Key und ein Value hat. Sorry falls es ein wenig kompliziert geschrieben ist. :hells: Gruß Nightfall
28. April 200916 j class Program { public struct CommandStruct { public string aCategory; // Eine Category kann mehrere Commands haben. public string aCommand; // Ein Command kann nur eine Category haben. (Jedes Command ist eingigartig) public string aShortcut; // Ein Shortcut kann nur zu einem Command gehören. public string aToolTip; // Mehrere Commands können einen gleichen ToolTip haben. public string aImage; // Mehrere Commands können sich ein Image teilen. } static void Main(string[] args) { List<CommandStruct> m_CommandStructList = new List<CommandStruct>(); m_CommandStructList.Add(new CommandStruct() { aCategory = "Cat1", aCommand = "Com1", aShortcut = "Strg+A", aImage = "Image1", aToolTip = "fdbgio" }); m_CommandStructList.Add(new CommandStruct() { aCategory = "Cat1", aCommand = "Com2", aShortcut = "Strg+B", aImage = "Image2", aToolTip = "fdbgfhrjio" }); m_CommandStructList.Add(new CommandStruct() { aCategory = "Cat2", aCommand = "Com3", aShortcut = "Strg+C", aImage = "Image1", aToolTip = "trh" }); m_CommandStructList.Add(new CommandStruct() { aCategory = "Cat2", aCommand = "Com4", aShortcut = "Strg+D", aImage = "Image2", aToolTip = "fdbgio" }); m_CommandStructList.Add(new CommandStruct() { aCategory = "Cat2", aCommand = "Com5", aShortcut = "Strg+E", aImage = "Image1", aToolTip = "fdbfjjgio" }); var shortcut = from com in m_CommandStructList where com.aCategory == "Cat1" && com.aCommand == "Com2" select com.aShortcut; Console.WriteLine(shortcut.First()); var commands = from com in m_CommandStructList where com.aCategory == "Cat2" select com; foreach (CommandStruct cs in commands) { Console.WriteLine(cs.aCommand); } } }[/PHP]
28. April 200916 j Habe es auch mal mit mehreren Dictionarys probiert. Das hat aber den Nachteil das man immer nur einen Key und ein Value hat. List<CommandStruct> als Value?! :floet: Aber ich denke das Linq-Beispiel vom Piraten ist da performanter, einfacherer usw.
Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.