enum KnownError
{
[StringValue("CODE-001")]
CODE001,
[StringValue("CODE-002")]
CODE002,
[StringValue("CODE-003")]
CODE003,
[StringValue("CODE-004")]
CODE004,
[StringValue("CODE-005")]
CODE005
}
List<string> errors = {"ahah", "eheh", "CODE-005", "uhuh"};
Let's say i have a list of string errors. How can I check if any error is "known"?
bool ContainsKnownError(List<string> error)
{
return errors.Where(error => Enum.IsDefined(typeof(KnownError), error) == true).Count() > 0;
}
This doesn't seem to work. How can I access StringValue inside the linq query without having to compare each string?
EDIT
I tried @AK_ solution, using Intersect, but I'm getting this compilation error:
The type arguments for method 'System.Linq.Enumerable.Intersect<TSource>(System.Collections.Generic.IEnumerable<TSource>, System.Collections.Generic.IEnumerable<TSource>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
The real scenario is an Error object with a string field with the code like this
class Error { string code; }
List<Error> errors = GetErrors();
var knownErrors = Enum.GetValues(typeof(KnownError));
bool exists = errors.Select(error => error.code).Intersect(knownErrors).Any();