Rich
Rich

Reputation: 6561

How to find an error in Linq using PropertyValidationErrors when key is not found in results

I have the following code which will find an error if results contains the key. It will then select the count of errors:

var foundError = results
    .Where(e => e.PropertyValidationErrors.Keys.Contains(id))
     .Select(e => e.PropertyValidationErrors[id]).ToList();

I want to instead find an error if the id is not found in the results. I'm not sure of the syntax. I want to do something like this:

// This is not correct, but similar to what I want to do
var foundError = results
    .WhereNot(e => e.PropertyValidationErrors.Keys.Contains(id))
     .Select(e => e.PropertyValidationErrors[id]).ToList();

Please advise on the syntax. Thanks

Upvotes: 0

Views: 43

Answers (1)

Nzall
Nzall

Reputation: 3565

I think you can just do

var foundError = results
    .Where(e => !e.PropertyValidationErrors.Keys.Contains(id))
    .Select(e => e.PropertyValidationErrors).ToList();

and it will select all PropertyValidationErrors for objects that don't contain a propertyValidationError with the selected key.

Upvotes: 1

Related Questions