我有这些课程。为了保持简单,我遗漏了与问题无关的成员。我想找到包含具有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));
答案 0 :(得分:3)
过滤掉空项:
var selection3 = VSANDictionary
.SelectMany(vsan => vsan.Value.ActiveZoneset.ZoneList)
.Select(z => z.ContainsMemberWWPN(zonemember))
.Where(m=> m != null)