Поиск в List со сложной структурой - C#

Узнай цену своей работы

Формулировка задачи:

Всем привет, интересует такой вопрос. У меня есть такая струкутра
Листинг программы
  1. ----List<IServer>
  2. --------|
  3. -------List<INode( TCP или RTU)>
  4. ------------|
  5. -----------List<IDevice>
  6. ----------------|
  7. ---------------List<(IGroup или ITag)>
  8. --------------------|
  9. -------------------List<ITag>
Так вот работаю только с List<IServer>, по нему чтобы найти например какой-либо Tag мне приходиться делать такой поиск:
Листинг программы
  1. foreach (Server z in List)
  2. {
  3. foreach (INode node in z.Nodes)
  4. {
  5. TCP _node = (TCP)node;
  6. foreach (Device device in _node.Devices)
  7. {
  8. IEnumerable<IGroup> d = device.Groups.Where(p => p.GetType == typeof(Group));
  9. foreach (Group group in d)
  10. {
  11. var n = group.Tags.Where(p => p.Name == e.Node.Text);
  12. foreach (ITag tag in n)
  13. {
  14. tag.Name = newname;
  15. }
  16. }
  17. }
  18. }
  19. }
возможно ли как-то упростить поиск? подскажите

Решение задачи: «Поиск в List со сложной структурой»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Ports;
  4. using System.Linq;
  5.  
  6. namespace ConsoleApplication198
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             var tag = new Tag(null, default(TypeData), default(TypeModbus), "");
  13.             var group = new Group("", new List<ITag>(){tag});
  14.             var device = new Device(2343, "myDevice", new List<IGroup>(){group});
  15.             var tcp = new TCP("", "", 333, new List<IDevice>(){device});
  16.             var server = new Server("server_name", new List<INode>(){tcp});
  17.  
  18.             var found = server.AllChilds().OfType<Device>().Where(d => d.Name == "myDevice");
  19.  
  20.             foreach (var d in found)
  21.                 Console.WriteLine(d.Name);
  22.  
  23.             Console.ReadKey();
  24.         }
  25.     }
  26.  
  27.     public interface ITag : IRegister
  28.     {
  29.         string Name { get; set; }
  30.         string SystemName { get; }
  31.     }
  32.  
  33.     public interface ISignal : IRegister
  34.     {
  35.         Type GetType { get; }
  36.     }
  37.  
  38.     public interface IRegister
  39.     {
  40.     }
  41.  
  42.     public interface IGroup : IRegister
  43.     {
  44.         string Name { get; set; }
  45.         string SystemName { get; }
  46.         Type GetType { get; }
  47.     }
  48.  
  49.     public interface IDevice : IRegister
  50.     {
  51.         string Name { get; set; }
  52.         string SystemName { get; }
  53.     }
  54.  
  55.     public interface IServer : IRegister
  56.     {
  57.         Type GetType { get; }
  58.     }
  59.  
  60.     public interface INode : IRegister
  61.     {
  62.         string Name { get; set; }
  63.         string SystemName { get; }
  64.         Type GetType { get; }
  65.     }
  66.  
  67.     [Serializable]
  68.     public class Server : IServer, ITreeNode
  69.     {
  70.         public string Name;
  71.         public string SystemName;
  72.         public List<INode> Nodes;
  73.  
  74.  
  75.         public Server(string name, List<INode> node)
  76.         {
  77.             Name = name;
  78.             Nodes = node;
  79.             SystemName = "Server";
  80.         }
  81.         public Type GetType
  82.         {
  83.             get { return typeof(Server); }
  84.         }
  85.  
  86.         public IEnumerable<ITreeNode> Childs
  87.         {
  88.             get { return Nodes.Cast<ITreeNode>(); }
  89.         }
  90.     }
  91.  
  92.     [Serializable]
  93.     public class TCP : INode, ITreeNode
  94.     {
  95.         public string IPAddress;
  96.         public int Port;
  97.         public List<IDevice> Devices;
  98.         public string Name { get; set; }
  99.         public string SystemName { get; private set; }
  100.  
  101.         public TCP(string name, string ip, int port, List<IDevice> devices)
  102.         {
  103.             Name = name;
  104.             IPAddress = ip;
  105.             Port = port;
  106.             Devices = devices;
  107.             SystemName = "Node";
  108.         }
  109.  
  110.         public TCP()
  111.         {
  112.         }
  113.  
  114.         public Type GetType
  115.         {
  116.             get { return typeof(TCP); }
  117.         }
  118.  
  119.         public IEnumerable<ITreeNode> Childs
  120.         {
  121.             get { return Devices.Cast<ITreeNode>(); }
  122.         }
  123.     }
  124.  
  125.     [Serializable]
  126.     public class RTU : INode, ITreeNode
  127.     {
  128.         public string Name { get; set; }
  129.         public string SystemName { get; private set; }
  130.         public string Port;
  131.         public int SpeedRate;
  132.         public int DataBits;
  133.         public int StopBits;
  134.         public Parity Parity;
  135.         public List<IDevice> Devices;
  136.  
  137.         public RTU(string name, int sr, int db, int sb, Parity par, string port, List<IDevice> devices)
  138.         {
  139.             Name = name;
  140.             Port = port;
  141.             SpeedRate = sr;
  142.             StopBits = sb;
  143.             DataBits = db;
  144.             Devices = devices;
  145.             Parity = par;
  146.             SystemName = "Node";
  147.         }
  148.  
  149.         public Type GetType
  150.         {
  151.             get { return typeof(RTU); }
  152.         }
  153.  
  154.         public IEnumerable<ITreeNode> Childs
  155.         {
  156.             get { return Devices.Cast<ITreeNode>(); }
  157.         }
  158.     }
  159.  
  160.     [Serializable]
  161.     public class Device : IDevice, ITreeNode
  162.     {
  163.         public int Address;
  164.         public string Name { get; set; }
  165.         public string SystemName { get; private set; }
  166.         public List<IGroup> Groups;
  167.  
  168.         public Device(int address, string name, List<IGroup> groups)
  169.         {
  170.             Address = address;
  171.             Groups = groups;
  172.             Name = name;
  173.             SystemName = "Device";
  174.         }
  175.  
  176.         public static Type GetType
  177.         {
  178.             get { return Device.GetType; }
  179.         }
  180.  
  181.         public Type DeviceType
  182.         {
  183.             get { return Groups[0].GetType(); }
  184.         }
  185.  
  186.         public IEnumerable<ITreeNode> Childs
  187.         {
  188.             get { return Groups.Cast<ITreeNode>(); }
  189.         }
  190.     }
  191.  
  192.     [Serializable]
  193.     public class Group : IGroup, ITreeNode
  194.     {
  195.         public List<ITag> Tags;
  196.         public string Name { get; set; }
  197.         public string SystemName { get; private set; }
  198.        
  199.         public Group(string name, List<ITag> tags)
  200.         {
  201.             Tags = tags;
  202.             Name = name;
  203.             SystemName = "Group";
  204.         }
  205.  
  206.         public Type GetType
  207.         {
  208.             get { return typeof(Group); ; }
  209.         }
  210.  
  211.         public IEnumerable<ITreeNode> Childs
  212.         {
  213.             get { return Tags.Cast<ITreeNode>(); }
  214.         }
  215.     }
  216.  
  217.     [Serializable]
  218.     public class Tag : ITag, IGroup, ITreeNode
  219.     {
  220.         public ISignal Signal;
  221.         public TypeData Data;
  222.         public TypeModbus TypeModbus;
  223.         //public object Value;
  224.         //public DateTime Time;
  225.         public string Name { get; set; }
  226.         public string SystemName { get; private set; }
  227.  
  228.         public Tag(ISignal signal, TypeData data, TypeModbus typeModbus,string n, object value = null)
  229.         {
  230.             Signal = signal;
  231.             Data = data;
  232.             TypeModbus = typeModbus;
  233.             //Value = value;
  234.             //Time = time;
  235.             Name = n;
  236.             SystemName = "Tag";
  237.         }
  238.  
  239.  
  240.         public Type GetType
  241.         {
  242.             get { return typeof(Tag); }
  243.         }
  244.  
  245.         public IEnumerable<ITreeNode> Childs
  246.         {
  247.             get { yield break; }
  248.         }
  249.     }
  250.  
  251.     [Serializable]
  252.     public class Analog : ISignal
  253.     {
  254.         public int Address;
  255.         public int Address_validaty;
  256.         public float MinWarning;
  257.         public float MinEmergency;
  258.         public float MaxWarning;
  259.         public float MaxEmergency;
  260.         public bool Control;
  261.         public float Coeficient;
  262.         public float Shift;
  263.         public bool IsCoeficient;
  264.         public string MinWText;
  265.         public string MinEText;
  266.         public string MaxWText;
  267.         public string MaxEText;
  268.  
  269.         public Type GetType
  270.         {
  271.             get { return typeof(Analog); }
  272.         }
  273.     }
  274.  
  275.     [Serializable]
  276.     public class Discrete : ISignal
  277.     {
  278.         public int Address;
  279.         public int Address_validaty;
  280.         public bool IsAutomat;
  281.         public static ITag Tag = null;
  282.         public string TrueText;
  283.         public string FalseText;
  284.         public Discrete(int ad, int adv,  bool isautomat, string ft, string tt, ITag tag = null)
  285.         {
  286.             Address = ad;
  287.             Address_validaty = adv;
  288.             TrueText = tt;
  289.             FalseText = ft;
  290.             IsAutomat = isautomat;
  291.  
  292.             if (isautomat)
  293.                 Tag = tag;
  294.         }
  295.         public Type GetType
  296.         {
  297.             get { return typeof(Discrete); }
  298.         }
  299.  
  300.     }
  301.  
  302.     [Serializable]
  303.     public class Managment : ISignal
  304.     {
  305.         public ITag ConnectionRegister;
  306.         public int Address;
  307.         public int SecondsReply;
  308.  
  309.  
  310.         public Type GetType
  311.         {
  312.             get { return typeof(Managment); }
  313.         }
  314.     }
  315.  
  316.     public interface ITreeNode
  317.     {
  318.         IEnumerable<ITreeNode> Childs { get; }
  319.     }
  320.  
  321.     public static class NodeHelper
  322.     {
  323.         public static IEnumerable<ITreeNode> AllChilds(this ITreeNode root)
  324.         {
  325.             yield return root;
  326.             foreach (var child in root.Childs)
  327.                 foreach (var c in child.AllChilds())
  328.                     yield return c;
  329.         }
  330.     }
  331. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

6   голосов , оценка 4 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут