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 6843453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:14:48+00:00 2026-05-27T00:14:48+00:00

I have a method that gets a list of objects and then returns all

  • 0

I have a method that gets a list of objects and then returns all their properties values based on the attributes and types of the property.
I’m having difficulties with selecting the value because the Select depends on conditions.
What I did so far allows me to get the value in the ugliest way I have ever seen:

public IEnumerable<string> GetValues(List<ProperyInfo> objects)
{
     var allValues = new List<string>();
     foreach (var obj in objects)
     {
         // Get the PropertyInfo for the obj
         var properties = GetPropertiesWithTheAttributes(obj);
         var values = properties.Select(x => new 
             {
             Value = x.GetValue(obj, null) == null
                         ?  string.empty
                         :
                         x.PropertyType.BaseType == typeof(Enum)
                         ? Convert.ToInt32(x.GetValue(obj, null)).ToString()
                         : (x.GetValue(obj, null)).ToString(),
             ((ReportAttribute)
             Attribute.GetCustomAttribute(x, typeof(ReportAttribute), false)).Order
             })
             .OrderBy(x => x.Order)
             .Select(x => x.Value.ToString())
             .ToList();

             allValues.AddRange(values);
    }

    return allValues;
}

And in the code I published here I even removed the check for the DisplayDate property in the ReportAttribute, which verifies if the datetime property of the attribute would be displayed as date or datetime…

serenity now!

  • 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-05-27T00:14:49+00:00Added an answer on May 27, 2026 at 12:14 am

    I would simply extract this into 2 methods, and get rid of the ternary operator:

        // not sure how to name 'obj' and 'x' without more context
        private static object GetValue(PropertyInfo obj, PropertyInfo x)
        {
            if (x.GetValue(obj, null) == null) return string.Empty;
    
            if (x.PropertyType.BaseType == typeof (Enum))
                return Convert.ToInt32(x.GetValue(obj, null));
    
            return x.GetValue(obj, null);
        }
    
        private static int GetOrder(PropertyInfo x)
        {
            return ((ReportAttribute) Attribute.GetCustomAttribute(x, typeof(ReportAttribute), false)).Order;
        }
    

    So you can write:

        public IEnumerable<string> GetValues(List<PropertyInfo> objects)
        {
            var allValues = new List<string>();
            foreach (var obj in objects)
            {
                // Get the PropertyInfo for the obj 
                var properties = GetPropertiesWithTheAttributes(obj);
                var values = properties.Select(x => new
                                                        {
                                                            Value = GetValue(obj, x),
                                                            Order = GetOrder(x)
                                                        })
                    .OrderBy(x => x.Order)
                    .Select(x => x.Value.ToString())
                    .ToList();
    
                allValues.AddRange(values);
            }
    
            return allValues;
        }
    

    If you really want to inline this, you can change the lambda expression in your Select to a statement:

    .Select(x =>
                {
                    object value;
                    if (x.GetValue(obj, null) == null) value = string.Empty;
                    else if (x.PropertyType.BaseType == typeof (Enum))
                        value = Convert.ToInt32(x.GetValue(obj, null));
                    else value = x.GetValue(obj, null);
    
                    return new
                                {
                                    Value = value,
                                    ((ReportAttribute)
                                    Attribute.GetCustomAttribute(x, typeof (ReportAttribute), false)).
                                        Order
                                };
                })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class method that returns a list of employees that I can iterate
I have an ASP.NET webservice method that returns a generics list (List'<'Construct>) serialized as
We have a simple method that gets all of a specific domain object where
I have a AJAX post method that gets xml data from server. But when
I have a very basic doubt regarding the method that gets executed when app
I have a list that gets refreshed every 2 seconds via the Handler postDelayed()
I have a method that gets called when entering a chart page in my
I have a shared instance of a List that gets updated randomly by various
I have a method that where I want to redirect the user back to
I have a method that can return either a single object or a collection

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.