1

I have a list of objects like this:

var obj =
[
    {a : [{v:1},{x:1}] },
    {a : [{y:1},{t:2}] },
    {a : [{z:1},{s:3}] }
]

I want to select only objects that has a list of elements, and at least one of which has property "x". In this case, I want {a : [{v:1},{x:1}] } to be returned by the LINQ. Something like

obj.Where(s => 
    foreach(var o in s.a){ 
        if(o.x) return true; 
    }
    return false;
)

1 Answer 1

1

You're looking for .Any()

obj.Where(s => s.a.Any(o => o.x)).ToList());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.