Then you have a list of names
of days, filled with only 3 names and need all the other days
First of all, You are trying to compare string
values to WeekDays
values, where the strings are supposed to be names of the WeekDays
List<string> days = new List<string>();
You cannot compare them without a conversion. The conversion can be either direction, depending on reuse chances.
- You can gather the names of all WeekDays
values, using Enum.GetNames
, in case storing in a temporary variable or in a static field to avoid repeating the operation.
- You can convert names of days
list to WeekDays
values, using Enum.Parse
, and store in a temporary variable; in this case, remember that Enum.Parse
will throw on unrecognized/bad names.
In both cases you can use System.Linq.Enumerable.Except
Get all missing names as array, then convert them to values:
var missingNames = Enum.GetNames(typeof(WeekDays)).Except(days).ToArray();
var missingValues = Array.ConvertAll(missingNames, value => (WeekDays)Enum.Parse(typeof(WeekDays), value, true));
Converts used names to values as enumerable sequence, then get all missing values as array:
var daysValues = days.Select(value => (WeekDays)Enum.Parse(typeof(WeekDays), value, true));
var missingValues = ((WeekDays)Enum.GetValues(typeof(WeekDays))).Except(daysValues).ToArray();
These are the most compact solutions, but I would break them into their composing steps, both for readability and maintenability purposes.
I suggest you to look into online help contents and local documentation for all the methods (Enum.Parse, Enum.TryParse, Enum.GetNames, Enum.GetValues, Array.ConvertAll, Enumerable.Select, Enumerable.Except, Enumerable.ToArray).
Keep in mind that these solutions produce array results, that are best suited in case you will perform several operations on them, and to debug them; if you will iterate only once, than at least the last ToArray
invocation is not needed.
method to work, you have to add using System.Linq;
on top of the source file.