Results 1 to 2 of 2

Thread: Looking for simplier or shorter way to convert enum description attributes to a list

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Looking for simplier or shorter way to convert enum description attributes to a list

    Sure there is a better way to accomplish this. Perhaps with a linq statement?

    Im wanting to geta a list of all the descriptions on the enum which will be thrown into a dropdown in grid column

    I cant just go by teh names and convert to a string as I have a situation where there is a "blank" and no way to represent that as a named enum member which is a valid option

    Thanks


    Code:
    //Controller
            [HttpGet]
            public JsonResult GetPriorities([DataSourceRequest]DataSourceRequest request)
            {
                List<string> model = new List<string>();
                List<CaseParty.PRIORITY_TYPE> enm = Enum.GetValues(typeof(CaseParty.PRIORITY_TYPE)).Cast<CaseParty.PRIORITY_TYPE>().ToList();
                foreach (CaseParty.PRIORITY_TYPE en in enm)
                {
                    string attrib = en.ToDescriptionString();
                    model.Add(attrib);
                }
                return Json(model, JsonRequestBehavior.AllowGet);
            }
    Code:
    //Extension method
            public static string ToDescriptionString(this Enum This)
            {
                Type type = This.GetType();
                string name = Enum.GetName(type, This);
                MemberInfo member = type.GetMembers().Where(w => w.Name == name).FirstOrDefault();
                DescriptionAttribute attribute = member != null ? member.GetCustomAttributes(true).Where(w => w.GetType() == typeof(DescriptionAttribute))
                        .FirstOrDefault() as DescriptionAttribute : null;
    
                return attribute != null ? attribute.Description : name;
            }
    Code:
    //ENUM
            public enum PRIORITY_TYPE
            {
                [Description(" ")]
                BLANK,
                [Description("A")]
                A,
                [Description("B")]
                B,
                [Description("C")]
                C
            }
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Looking for simplier or shorter way to convert enum description attributes to a l

    The code is not necessarily shorter overall but check out these classes I created for this purpose:

    http://www.vbforums.com/showthread.php?552563

    In your case, the code would be:
    csharp Code:
    1. var model = new EnumDescriptorCollection<CaseParty.PRIORITY_TYPE>().Select(ed => ed.ToString()).ToList();
    That said, you could already be using this:
    csharp Code:
    1. var model = Enum.GetValues(typeof(CaseParty.PRIORITY_TYPE))
    2.                 .Cast<CaseParty.PRIORITY_TYPE>()
    3.                 .Select(pt => pt.ToDescriptionString())
    4.                 .ToList();
    Last edited by jmcilhinney; Feb 22nd, 2019 at 10:40 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width