trailerman
trailerman

Reputation: 321

Get list based on condition from another generic list

I have 2 classes called Config and ClassField. The config class has a generic list of ClassFields. ClassFields has a generic list of strings called ClassErrors.

I have a List<Config> in a variable and would like to get only the Configs which do not have any Class Errors.

I have tried the following code but just can't seem to get it right.

var list = _lstSyncConfigs.Where(f => f.SyncConfigClassFields.Where(g => g.AttributeErrors.Count == 0).Select(f).ToList();

Upvotes: 0

Views: 54

Answers (1)

TheGeneral
TheGeneral

Reputation: 81483

This maybe what you are looking for. Where > All > not Any

var list = _lstSyncConfigs.Where(f => f.SyncConfigClassFields.All(g => !g.AttributeErrors.Any()))
                          .ToList();
// or

var list = _lstSyncConfigs.Where(f => f.SyncConfigClassFields.All(g => g.AttributeErrors.Count == 0))
                          .ToList()

Note : If one of your lists has the potential to be null, you might wont to use the Null-Conditional Operator

Upvotes: 3

Related Questions