Поиск в List со сложной структурой - C#
Формулировка задачи:
Всем привет, интересует такой вопрос.
У меня есть такая струкутра
Так вот работаю только с List<IServer>, по нему чтобы найти например какой-либо Tag мне приходиться делать такой поиск:
возможно ли как-то упростить поиск? подскажите
Листинг программы
- ----List<IServer>
- --------|
- -------List<INode( TCP или RTU)>
- ------------|
- -----------List<IDevice>
- ----------------|
- ---------------List<(IGroup или ITag)>
- --------------------|
- -------------------List<ITag>
Листинг программы
- foreach (Server z in List)
- {
- foreach (INode node in z.Nodes)
- {
- TCP _node = (TCP)node;
- foreach (Device device in _node.Devices)
- {
- IEnumerable<IGroup> d = device.Groups.Where(p => p.GetType == typeof(Group));
- foreach (Group group in d)
- {
- var n = group.Tags.Where(p => p.Name == e.Node.Text);
- foreach (ITag tag in n)
- {
- tag.Name = newname;
- }
- }
- }
- }
- }
Решение задачи: «Поиск в List со сложной структурой»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.IO.Ports;
- using System.Linq;
- namespace ConsoleApplication198
- {
- class Program
- {
- static void Main(string[] args)
- {
- var tag = new Tag(null, default(TypeData), default(TypeModbus), "");
- var group = new Group("", new List<ITag>(){tag});
- var device = new Device(2343, "myDevice", new List<IGroup>(){group});
- var tcp = new TCP("", "", 333, new List<IDevice>(){device});
- var server = new Server("server_name", new List<INode>(){tcp});
- var found = server.AllChilds().OfType<Device>().Where(d => d.Name == "myDevice");
- foreach (var d in found)
- Console.WriteLine(d.Name);
- Console.ReadKey();
- }
- }
- public interface ITag : IRegister
- {
- string Name { get; set; }
- string SystemName { get; }
- }
- public interface ISignal : IRegister
- {
- Type GetType { get; }
- }
- public interface IRegister
- {
- }
- public interface IGroup : IRegister
- {
- string Name { get; set; }
- string SystemName { get; }
- Type GetType { get; }
- }
- public interface IDevice : IRegister
- {
- string Name { get; set; }
- string SystemName { get; }
- }
- public interface IServer : IRegister
- {
- Type GetType { get; }
- }
- public interface INode : IRegister
- {
- string Name { get; set; }
- string SystemName { get; }
- Type GetType { get; }
- }
- [Serializable]
- public class Server : IServer, ITreeNode
- {
- public string Name;
- public string SystemName;
- public List<INode> Nodes;
- public Server(string name, List<INode> node)
- {
- Name = name;
- Nodes = node;
- SystemName = "Server";
- }
- public Type GetType
- {
- get { return typeof(Server); }
- }
- public IEnumerable<ITreeNode> Childs
- {
- get { return Nodes.Cast<ITreeNode>(); }
- }
- }
- [Serializable]
- public class TCP : INode, ITreeNode
- {
- public string IPAddress;
- public int Port;
- public List<IDevice> Devices;
- public string Name { get; set; }
- public string SystemName { get; private set; }
- public TCP(string name, string ip, int port, List<IDevice> devices)
- {
- Name = name;
- IPAddress = ip;
- Port = port;
- Devices = devices;
- SystemName = "Node";
- }
- public TCP()
- {
- }
- public Type GetType
- {
- get { return typeof(TCP); }
- }
- public IEnumerable<ITreeNode> Childs
- {
- get { return Devices.Cast<ITreeNode>(); }
- }
- }
- [Serializable]
- public class RTU : INode, ITreeNode
- {
- public string Name { get; set; }
- public string SystemName { get; private set; }
- public string Port;
- public int SpeedRate;
- public int DataBits;
- public int StopBits;
- public Parity Parity;
- public List<IDevice> Devices;
- public RTU(string name, int sr, int db, int sb, Parity par, string port, List<IDevice> devices)
- {
- Name = name;
- Port = port;
- SpeedRate = sr;
- StopBits = sb;
- DataBits = db;
- Devices = devices;
- Parity = par;
- SystemName = "Node";
- }
- public Type GetType
- {
- get { return typeof(RTU); }
- }
- public IEnumerable<ITreeNode> Childs
- {
- get { return Devices.Cast<ITreeNode>(); }
- }
- }
- [Serializable]
- public class Device : IDevice, ITreeNode
- {
- public int Address;
- public string Name { get; set; }
- public string SystemName { get; private set; }
- public List<IGroup> Groups;
- public Device(int address, string name, List<IGroup> groups)
- {
- Address = address;
- Groups = groups;
- Name = name;
- SystemName = "Device";
- }
- public static Type GetType
- {
- get { return Device.GetType; }
- }
- public Type DeviceType
- {
- get { return Groups[0].GetType(); }
- }
- public IEnumerable<ITreeNode> Childs
- {
- get { return Groups.Cast<ITreeNode>(); }
- }
- }
- [Serializable]
- public class Group : IGroup, ITreeNode
- {
- public List<ITag> Tags;
- public string Name { get; set; }
- public string SystemName { get; private set; }
- public Group(string name, List<ITag> tags)
- {
- Tags = tags;
- Name = name;
- SystemName = "Group";
- }
- public Type GetType
- {
- get { return typeof(Group); ; }
- }
- public IEnumerable<ITreeNode> Childs
- {
- get { return Tags.Cast<ITreeNode>(); }
- }
- }
- [Serializable]
- public class Tag : ITag, IGroup, ITreeNode
- {
- public ISignal Signal;
- public TypeData Data;
- public TypeModbus TypeModbus;
- //public object Value;
- //public DateTime Time;
- public string Name { get; set; }
- public string SystemName { get; private set; }
- public Tag(ISignal signal, TypeData data, TypeModbus typeModbus,string n, object value = null)
- {
- Signal = signal;
- Data = data;
- TypeModbus = typeModbus;
- //Value = value;
- //Time = time;
- Name = n;
- SystemName = "Tag";
- }
- public Type GetType
- {
- get { return typeof(Tag); }
- }
- public IEnumerable<ITreeNode> Childs
- {
- get { yield break; }
- }
- }
- [Serializable]
- public class Analog : ISignal
- {
- public int Address;
- public int Address_validaty;
- public float MinWarning;
- public float MinEmergency;
- public float MaxWarning;
- public float MaxEmergency;
- public bool Control;
- public float Coeficient;
- public float Shift;
- public bool IsCoeficient;
- public string MinWText;
- public string MinEText;
- public string MaxWText;
- public string MaxEText;
- public Type GetType
- {
- get { return typeof(Analog); }
- }
- }
- [Serializable]
- public class Discrete : ISignal
- {
- public int Address;
- public int Address_validaty;
- public bool IsAutomat;
- public static ITag Tag = null;
- public string TrueText;
- public string FalseText;
- public Discrete(int ad, int adv, bool isautomat, string ft, string tt, ITag tag = null)
- {
- Address = ad;
- Address_validaty = adv;
- TrueText = tt;
- FalseText = ft;
- IsAutomat = isautomat;
- if (isautomat)
- Tag = tag;
- }
- public Type GetType
- {
- get { return typeof(Discrete); }
- }
- }
- [Serializable]
- public class Managment : ISignal
- {
- public ITag ConnectionRegister;
- public int Address;
- public int SecondsReply;
- public Type GetType
- {
- get { return typeof(Managment); }
- }
- }
- public interface ITreeNode
- {
- IEnumerable<ITreeNode> Childs { get; }
- }
- public static class NodeHelper
- {
- public static IEnumerable<ITreeNode> AllChilds(this ITreeNode root)
- {
- yield return root;
- foreach (var child in root.Childs)
- foreach (var c in child.AllChilds())
- yield return c;
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д