Click here to Skip to main content
16,019,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
if i have an enum

enum days
{
sun
mon
tue
wed
thu
fri
sat
}

and i have a list of days called D that contains sun,mon,tue,wed

i want to return the enum ids that are not used, so i need the ids of thu,fri and sat


What I have tried:

i was able to get the names itself but i couldn't get the ids


<pre>  
 List<string> Days = Enum.GetNames(typeof(WeekDay)).ToList();
 var closed = Days.Except(D); // D is the list of days i mentioned above


so closed now have thur,fri and sat but i want the ids so what can i replace Enum.getNames with?
Posted
Updated 21-Apr-20 21:37pm

It depends on what D contains: at the moment it contains strings, so you would have to convert the strings back to Enum values:
C#
var closed = Days.Except(D).Select(d => Enum.Parse(typeof(days), d));
 
Share this answer
 
Either your list D is already of type days, then
{
    List<days> Days = Enum.GetValues(typeof(days)).OfType<days>().ToList();
    var D = new days[] { days.sun, days.mon, days.tue, days.wed };
    var closed = Days.Except(D);
}

or it consists of strings, then
{
    List<days> Days = Enum.GetValues(typeof(days)).OfType<days>().ToList();
    List<(days, string)> DaysAndNames = Days.Select(day => (day, Enum.GetName(typeof(days), day))).ToList();
    var D = new string[] {"sun", "mon", "tue", "wed" };
    var DAsdays = D.Select(d => DaysAndNames.Where(dn => dn.Item2 == d).Select(dn => dn.Item1).FirstOrDefault()).ToList();
    var closed = Days.Except(DAsdays);
}

Good luck!
 
Share this answer
 
Comments
BillWoodruff 23-Apr-20 2:57am    
do keep in mind that "beginners" may struggle with things like the use of 'Tuples if their use in an answer is not explained. a simple solution may be more useful than an elaborate one.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900