0

So I have a enumflag system and I need to filter by Narcotic, non-Narcotic, Psychotropic, and non-Psychotropic in a drop down list. My thinking was to put the values in dictionary and they viewbag into a selectlist on the front end, but I am having trouble configuring the dictionary to register "the absense of [var]"

If my dictionary is structured as thus:

private readonly Dictionary<int, string> _medicationDetails = new Dictionary<int, string>
        {
            {(int)PersonMedicationDescription.MedicationTags.NarcoticDrug, "Narcotic"},
            {(int)PersonMedicationDescription.MedicationTags.PsychotropicDrug, "Psychotropic"}
        };

I want to be able to do:

{(int)!PersonMedicationDescription.MedicationTags.NarcoticDrug, "non-Narcotic"},

or something along those lines. What am I missing here? Is there a better way to accomplish this?

EDIT:

Is a bool the right way to go. I know how to do that if it were just one bool, but how do I get both to populate the list? To get one to work I think this would work:

ViewBag.IsNarcoticOptions = new[]
            {
                true,
                false                
            }.ToSelectList(b => b.ToString(), b => b.ToString("Narcotic", "Non Narcotic"));

var isNarcotic = filters.IsNarcotic;
            if (isNarcotic.HasValue)
            {
                query = isNarcotic.Value
                    ? query.Where(rdq => (rdq.MedicationFlags & (int)PersonMedicationDescription.MedicationTags.NarcoticDrug) == (int)PersonMedicationDescription.MedicationTags.NarcoticDrug)
                    : query.Where(rdq => (rdq.MedicationFlags & (int)PersonMedicationDescription.MedicationTags.NarcoticDrug) == 0);
            }    

but how to do that for another set of true/false?

Mr9mm
  • 19
  • 3
  • What would your line (the last one) do? return true/false or add it if it's not there? – Haytam Jul 03 '18 at 14:11
  • I would want four select boxes in my dropdown. Narcotic, non-Narcotic, Psychotropic, non-Psychotropic. I thought about using a bool and ternarying them, but am not sure how to do that with 2 bools at once in concert with each other. – Mr9mm Jul 03 '18 at 14:17
  • So you don't have an Non-Narcotic & Non-Psychotropic enum values? – Kevin LaBranche Jul 03 '18 at 14:17
  • I do not. I suppose I could make some, but that seems pig fisted. There are already alot of enums in that category and I don't want to waste space for what might get more. – Mr9mm Jul 03 '18 at 14:22

1 Answers1

0

It seems that you are dealing with flags: a drug is either Narcotic or not, either Psychotropic; flags can be combined: we can well have Narcotic and Psychotropic (LSD?) or neither Psychotropic nor Narcotic (Aspirin). If you have few flags (less than 64) you can try designing the enum as Flags and get rid of dictionary

[Flags]
public enum MedicationTags {
  None = 0,
  Narcotic = 1,
  Psychotropic = 1 << 1,
  // SomeOtherKind = 1 << n // where n = 2, 3, 4 etc.
}

Then let's implement an extension method Description for the enum:

public static class MedicationTagsExtensions {
  public static String Description(this MedicationTags value) {
    return string.Join(", ",
      (value.HasFlag(MedicationTags.Narcotic) ? "" : "non-") + "Narcotic",
      (value.HasFlag(MedicationTags.Psychotropic) ? "" : "non-") + "Psychotropic"
    );
  }
}

So when having drug kind:

  // Morphine is narcotic only
  MedicationTags morphine = MedicationTags.Narcotic; 
  // LSD is both narcotic and psychotropic
  MedicationTags lsd = MedicationTags.Narcotic | MedicationTags.Psychotropic; 
  // Good old aspirin is neither narcotic nor psychotropic
  MedicationTags aspirin = MedicationTags.None; 

you can easily get description

  Console.WriteLine(aspirin.Description());

Outcome:

  non-Narcotic, non-Psychotropic
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215