2
我有这些类。为了简化起见,我忽略了与这个问题无关的成员。我想查找包含具有WWPN给定字符串值的成员的所有区域。下面的LINQ工作,但结果也包含空的不匹配的区域。我的其他尝试要么给我区域成员本身,要么给我一个布尔。有没有办法做到这一点,而没有得到空值?我不需要使用ContainsMemberWWPN()类成员。LINQ查询SelectMany()返回空值
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));
完美。谢谢。这比我想出的解决方案要干净得多。 –