0

I have a class structure like below.

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

From the observable collection object deviceCollection. I need to get the collection which matches with IsGroup property as false. So I have written like

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

Now the currentStruct should contain the collection basically List<TargetSelectionStructure>. I am unable to cast the currentStruct to the type of List<TargetSelectionStructure>.
How can I solve this?

1

1 Answer 1

0

You can't cast it, because currentStruct is an IEnumerable<Group>.

I think you query needs to look like this:

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

This returns all TargetSelectionStructure instances from all Groups that have IsGroup == false.

Your question is not entirely clear. It is possible to interpret your question in a second way: You want to have all TargetSelectionStructure instances from a Group if at least one of them has IsGroup == false.
To achieve this, you would use this query:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.