I have these classes. To keep it simpler I left out the members that don’t pertain to the question. I want to find all the zones that contain a member with a given string value for the WWPN. The LINQ below works but the results also contain null for the zones that don’t match. My other attempts either gave me the zone members themselves or a bool. Is there a way to do this without getting the null values? I don’t need to use the ContainsMemberWWPN() class member.
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));
Filter the null items out: