Resolve the linq expression error

I've a code like given below, but it gives a compile error "Cannot convert expression type 'System.Collections.Generic.List' to return type 'bool'

List<Condition> cod = MyRules.ToList().Where(r=>r.Conditions.ToList().Where(c=>c.id == 123).ToList();

Here I need to get all 'MyRules' which has conditions with 'id' = 123 Please help me to find out the correct way to right this expression. Thanks in advance.

Jon Skeet
people
quotationmark

Firstly, it's not clear why you're calling ToList all over the place. At the end, maybe... but no need for it any earlier. I think you're probably looking for the Any method:

List<Condition> cod = MyRules.Where(r => r.Conditions.Any(c => c.id == 123))
                             .ToList();

Note how writing one "top level" method call per line makes it easier to check brackets (your original code is missing at least one closing parenthesis) and easier to read in general.

If you want to find rules where all the conditions have an id of 123, you'd use All instead of Any.

people

See more on this question at Stackoverflow