Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要更多的程序逻辑帮助_C# - Fatal编程技术网

C# 需要更多的程序逻辑帮助

C# 需要更多的程序逻辑帮助,c#,C#,我有两个类的部分列表。CiscoSwitch对象维护到其他交换机的SwitchConnection对象列表。我正在尝试编写代码,遍历一个交换机列表,并返回具有公共连接的交换机,这些交换机可以相互连接,也可以返回其他交换机的连接列表中列出的相同RemoteSwitchName,但它可能不在我正在使用的CiscoSwitch列表中 例如,如果我有一个名为a、B和C的3个CiscoSwitch实例的列表,那么a和B可能相互连接。B和C可能没有直接连接到彼此,但两者都连接到D。A也可能连接到D。我正在将

我有两个类的部分列表。CiscoSwitch对象维护到其他交换机的SwitchConnection对象列表。我正在尝试编写代码,遍历一个交换机列表,并返回具有公共连接的交换机,这些交换机可以相互连接,也可以返回其他交换机的连接列表中列出的相同RemoteSwitchName,但它可能不在我正在使用的CiscoSwitch列表中

例如,如果我有一个名为a、B和C的3个CiscoSwitch实例的列表,那么a和B可能相互连接。B和C可能没有直接连接到彼此,但两者都连接到D。A也可能连接到D。我正在将每个交换机连接的RemoteSwitchName属性与列表中其他交换机的switchName属性以及其他交换机上其他交换机连接的RemoteSwitchName属性进行比较

如果可能的话,我更喜欢使用LINQ而不是大量的foreach循环

public CiscoSwitch 
{ 
private string _SwitchName = String.Empty
public string switchName{ get{return _SwitchName;} set{_SwitchName=value;} }        
 ...
public List<SwitchConnection> SwitchConnectionList = new List<SwitchConnection>();
...
 }

public class SwitchConnection
    // a switch connection is a connection to another switch
    // a switch connection can have multiple portchannels 
    // a switch connection can exist across multiple VSANs
{ 
   // the name of this switch
   // not needed, deprecated
  //  private string _LocalSwitchName;
  //  public string LocalSwitchName { get { return _LocalSwitchName; } set { _LocalSwitchName = value; } }

    // the name of the switch at the other end of the link
    private string _RemoteSwitchName;
    public string RemoteSwitchName { get { return _RemoteSwitchName; } set { _RemoteSwitchName = value; } }
    private string _RemoteIPAddress;
    public string RemoteIPAddress { get { return _RemoteIPAddress; } set { _RemoteIPAddress = value; } }
    public Dictionary<int, PortChannel> LocalPortChannelList = new Dictionary<int,PortChannel>();
}
公共CiscoSwitch
{ 
私有字符串_SwitchName=string.Empty
公共字符串switchName{get{return}switchName;}set{{switchName=value;}}
...
公共列表开关连接列表=新列表();
...
}
公共类交换连接
//交换机连接是与另一个交换机的连接
//交换机连接可以有多个端口通道
//交换机连接可以跨多个VSAN存在
{ 
//此开关的名称
//不需要,不推荐
//私有字符串\u LocalSwitchName;
//公共字符串LocalSwitchName{get{return{u LocalSwitchName;}set{{u LocalSwitchName=value;}}
//链接另一端的交换机的名称
私有字符串_RemoteSwitchName;
公共字符串RemoteSwitchName{get{return}RemoteSwitchName;}set{{u RemoteSwitchName=value;}}
私有字符串RemoteIPAddress;
公共字符串RemoteIPAddress{get{return}RemoteIPAddress;}set{RemoteIPAddress=value;}}
公共字典LocalPortChannelList=新字典();
}

私有字符串\u RemoteSwitchName
应该是对实际对象的引用,而不是“断开连接”的开关名称,我认为。那么获得名称是这样的:

protected SwitchConnection RemoteSwitch;

this.RemoteSwitch.Name;  //need to add "Name" property to the class, of course.
此引用还允许您相当轻松地遍历连接链。这与您查询收藏的想法并不相互排斥。然而,我认为,找到这个给定连接的“链”将比使用LINQ更容易

    // in SwitchConnection class
    public List<string> GetAllConnections(List<string> connectionChain) {

    // to start at any given object just pass in a null reference
    if (connectionChain == null) { connectionChain = new List<string>; }

    connectionChain.Add(this.Name);

    if (this.RemoteSwitch !=null) {
       RemoteSwitch.GetAllConnections(connectionChain);
    }

    return connectionChain;
}
//在SwitchConnection类中
公共列表GetAllConnections(列表连接链){
//要从任何给定对象开始,只需传入一个空引用
如果(connectionChain==null){connectionChain=new List;}
connectionChain.Add(this.Name);
如果(this.RemoteSwitch!=null){
RemoteSwitch.GetAllConnections(connectionChain);
}
返回连接链;
}
如果你认为你会有很多不同的查询,我认为一个单独的类可以让你的
CiscoSwitch
SwitchConnection
类保持干净。当您学习/实验LINQ时,您的“核心课程”不会因为LINQ学习错误而不断中断


您会注意到我没有显示任何LINQ。你得自己动手了。但是。

您给我们讲了一个故事,并向我们展示了一些代码。真正的问题是什么?你试过什么?-1:除了缺少问题部分外,问题的标题真的很糟糕。问题是如何做我想做的事。我认为这在我的帖子中是显而易见的。以问题“如何遍历交换机列表并将交换机之间的公共连接返回给彼此或可能不在列表中的交换机”的形式重申。至于我所尝试的,我在嵌套的foreach循环中做了几次尝试,但我认为在阅读@radarbob的帖子之后,我必须向类中添加更多的方法。我正在尝试绘制网络图。我不想把每个开关都拔出来。我只想画我有日志记录的开关,加上我有日志记录的开关之间的开关。花了几分钟的时间,我才知道你要做什么。RemoteSwichName属性可以是对实际开关对象的引用。哦,上面的代码将“ConnectionSwitch RemoteSwitch”作为SwitchConnection类的属性。您是说RemoteSwitch是CiscoSwitch类型的属性还是SwitchConnection类型的属性?我认为切换连接更有意义。对不起,我切换了开关和连接。我认为作为一个交换连接更有意义对