2012-08-23 7 views
2

나는 이러한 클래스가 있습니다. 간단하게 유지하기 위해 질문과 관련이없는 멤버는 빠뜨렸다. WWPN에 주어진 문자열 값을 갖는 구성원을 포함하는 모든 영역을 찾고 싶습니다. 아래의 LINQ는 작동하지만 결과에도 일치하지 않는 영역에 대해 null이 포함됩니다. 내 다른 시도는 나에게 존 멤버들 또는 불량배를 주었다. null 값을 가져 오지 않고이 작업을 수행 할 수있는 방법이 있습니까? ContainsMemberWWPN() 클래스 멤버를 사용할 필요가 없습니다.LINQ 쿼리 SelectMany() Null 값 반환

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)); 

답변

3

필터 널 항목 아웃 :

var selection3 = VSANDictionary 
       .SelectMany(vsan => vsan.Value.ActiveZoneset.ZoneList) 
       .Select(z => z.ContainsMemberWWPN(zonemember)) 
       .Where(m=> m != null) 
+1

완벽한. 감사. 이것은 내가 생각해 낸 해결책보다 훨씬 깔끔합니다. –