Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9244971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:09:34+00:00 2026-06-18T09:09:34+00:00

I created Enum ToFrendlyString function for my enums, but i cant use in Linq.

  • 0

I created Enum ToFrendlyString function for my enums, but i cant use in Linq.

 public enum MyEnum
    {

        Queued = 0,           
        [Description("In progress")]
        In_progress = 2,            
        [Description("No answer")]
        No_answer = 6,

    }



  public static class EnumToFrendlyString
    {

        public static string ToFrendlyString(this Enum value)
        {
            return value.GetEnumDescription();
        }


        public static string GetEnumDescription(this Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());

            var attributes =
                (DescriptionAttribute[])fi.GetCustomAttributes(
                    typeof(DescriptionAttribute),
                    false);

            if (attributes.Length > 0)
                return attributes[0].Description;

            return value.ToString();
        }
    }

When i try to use this function in Linq, im getting error

  var res = collection.AsQueryable().Where(p => p.UserID == UserID).OrderByDescending(p=> p.DateCreated).Select(p => new MyClass
                {                          
                     Date = p.DateCreated.ToString(),
                     Status = p.Status.ToFrendlyString(),                        
                }).Take(10).ToList();

If i make another function in same class, like

 private string MyStatusToString(MyEnum status)
       {
           return status.ToFrendlyString();
       }

and change my Linq to use this function, then everything works.

Error

Expression of type 'DAL.MyEnum' cannot be used for parameter of type 'System.Enum' of method 'System.String ToFrendlyString(System.Enum)'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:09:35+00:00Added an answer on June 18, 2026 at 9:09 am

    I’m not sure you can use Enum as the Type for an extension method like that – try this instead. I’ve taken the liberty of tidying the code up a bit, feel free to ignore those changes 🙂

    public static class EnumToFrendlyString
    {
        public static string ToFrendlyString<T>(this T value)
            where T : struct
        {
            return value.GetEnumDescription();
        }
    
        public static string GetEnumDescription<T>(this T value)
            where T : struct
        {
            return EnumDescriptionCache<T>.Descriptions[value];
        }
    
        private static class EnumDescriptionCache<T>
            where T : struct
        {
            public static Dictionary<T, string> Descriptions =
                Enum.GetValues(typeof(T))
                    .Cast<T>()
                    .ToDictionary(
                        value => value,
                        value => value.GetEnumDescriptionForCache());
        }
    
        private static string GetEnumDescriptionForCache<T>(this T value)
            where T : struct
        {
            if (!typeof(T).IsEnum)
            {
                throw new ArgumentException("Only use with enums", "value");
            }
    
            var descriptionAttribute = typeof(T)
                .GetField(value.ToString())
                .GetCustomAttributes(typeof(DescriptionAttribute), false)
                .Cast<DescriptionAttribute>()
                .FirstOrDefault();
    
            return (descriptionAttribute != null)
                ? descriptionAttribute.Description
                : value.ToString();
        }
    }
    

    I’ve added a private, generic class to cache the descriptions for your enum members to avoid lots of runtime use of reflection. It looks a bit odd popping in and out of the class to first cache then retrieve the values, but it should work fine 🙂

    The warning I gave in this answer still applies – the enum value passed to the dictionary isn’t validated, so you could crash it by calling ((MyEnum)5367372).ToFrendlyString().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im working on java, I have created an enum as follows: public enum myEnum
I understand I can create an enum like this: public enum MyEnum { ONE(1),
I need to store an object using Hibernate, but this object use an enum.
This is an elaboration on this question: c# Enum Function Parameters I created a
I have created two enums and I know they are not the same but
I have a simple Enum created public interface Params { public enum Locale {
I've created a SelectList from a enum. The enum has a description, and the
I have created a web service containing an enum with values as follows public
I created the following class to model a person: namespace DataBindingTest { public enum
Let's say I created the following class: public enum Position { Dealer(1), //1 SB(2),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.