c# - Check if string list contains any enum string value -
c# - Check if string list contains any enum string value -
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 have list of string errors. how can check if error "known"?
bool containsknownerror(list<string> error) { homecoming errors.where(error => enum.isdefined(typeof(knownerror), error) == true).count() > 0; } this doesn't seem work. how can access stringvalue within linq query without having compare each string?
edit
i tried @ak_ solution, using intersect, i'm getting compilation error:
the type arguments method 'system.linq.enumerable.intersect<tsource>(system.collections.generic.ienumerable<tsource>, system.collections.generic.ienumerable<tsource>)' cannot inferred usage. seek specifying type arguments explicitly. the real scenario error object string field code 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(); var knownerrors = enum.getvalues(typeof(knownerror)); homecoming errors.contains(error => knownerrors.contains(error)); //or cooler: homecoming errors.intersect(knownerrors).count() > 0;
zack's comment correct:return errors.intersect(knownerrors).any better... +1 him :-)
c# linq enums .net-3.5
Comments
Post a Comment