2012-10-12 2 views
0

다음과 같은 클래스 구조가 있습니다.람다 식 결과에서 일반 목록으로 캐스팅하는 방법

ObservableCollection<Group> deviceCollection = new ObservableCollection<Group>(); 
public class Group 
{ 
    public string Name { get; set; }  
    public List<TargetSelectionStructure> TargetCollection { get; set; } 
} 
public class TargetSelectionStructure 
{ 
    public string ItemId { get; set; } 
    public string Name { get; set; } 
    public bool IsGroup { get; set; } 
} 

observable collection 개체에서 deviceCollection. IsGroup 속성과 일치하는 컬렉션을 false로 가져와야합니다. 그래서 지금 currentStruct은 기본적으로 List<TargetSelectionStructure> 컬렉션을 포함해야

var currentStruct = deviceCollection.Where(d => d.TargetCollection.Any(t => t.IsGroup == false)); 

같이 작성했습니다. currentStruct를 List<TargetSelectionStructure> 유형으로 변환 할 수 없습니다.
어떻게 해결할 수 있습니까?

+0

사용은'ToList()'확장 메서드 http://msdn.microsoft.com/en-us/library/bb342261.aspx, 또는 :
는이를 달성하기 위해, 당신은이 쿼리를 사용합니다 '.'을 누르고 intellisense를 사용하십시오. 이를 위해 최소한 하나의 중복이 있어야합니다. – Jodrell

답변

0

currentStructIEnumerable<Group>이기 때문에 전송할 수 없습니다.

난 당신 쿼리는 다음과 같이 할 필요가 생각 :

var currentStruct = deviceCollection.SelectMany(x => x.TargetCollection) 
            .Where(x => !x.IsGroup).ToList(); 

IsGroup == false이있는 모든 Group의 모든 TargetSelectionStructure 인스턴스를 반환합니다.

귀하의 질문이 완전히 명확하지 않습니다. 두 번째 방법으로 질문을 해석 할 수 있습니다. TargetSelectionStructure 개의 인스턴스 중 하나 이상에 IsGroup == false이 있으면 Group의 모든 인스턴스를 갖고 싶습니다. 다만,

var currentStruct = deviceCollection.Where(x => x.TargetCollection 
               .Any(y => !y.IsGroup)) 
            .SelectMany(x => x.TargetCollection) 
            .ToList(); 
+0

고맙습니다. 그것은 효과가 있었다. 고마워 천재. –