2

我有这些课。为了简单起见,我省略了与问题无关的成员。我想找到包含具有给定字符串值的 WWPN 成员的所有区域。下面的 LINQ 有效,但结果也包含不匹配区域的 null。我的其他尝试要么给了我区域成员本身,要么给了我一个布尔值。有没有办法在不获取空值的情况下做到这一点?我不需要使用 ContainsMemberWWPN() 类成员。

  public class Zone
     {  ....
      public List<ZoneMember> MembersList = new List<ZoneMember>();
     }

  public class ZoneMember
   {
    private string _WWPN = string.Empty;
    public string MemberWWPN {get{return _WWPN;} set{_WWPN = value; } }
    private bool _IsLoggedIn;
    public bool IsLoggedIn { get { return _IsLoggedIn; } set { _IsLoggedIn = value; } }

    }

 public class CiscoVSAN
    {
      ....
    public List<Zone> ActiveZoneset = new List<Zone>(); 
            ....
    }

public Zone ContainsMemberWWPN(string wwpn)
    { 
      var contained = 
          this.MembersList.FirstOrDefault(m => m.MemberWWPN.Contains(wwpn));

      if (contained != null) { return this }
      else { return null; }

    }

//find all the zones that contain the input string
// this returns the zones that match
// but selection3 also has null values for zones that don't match
var selection3 = VSANDictionary.SelectMany(vsan => vsan.Value.ActiveZoneset.ZoneList).Select(z => z.ContainsMemberWWPN(zonemember));
4

1 回答 1

3

过滤掉空项:

var selection3 = VSANDictionary
                 .SelectMany(vsan => vsan.Value.ActiveZoneset.ZoneList)
                 .Select(z => z.ContainsMemberWWPN(zonemember))
                 .Where(m=> m != null)
于 2012-08-23T16:28:49.113 回答