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

  • Home
  • SEARCH
  • 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 568553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:12:02+00:00 2026-05-13T13:12:02+00:00

Let’s say you have a Generic Class which have a List<T> Items; Now think

  • 0

Let’s say you have a Generic Class which have a List<T> Items;

Now think of this basic lambda expression:

var result = Items.FindAll(x => x.Name = "Filip");

This will only work as long as we Know the Properties of T, which you don’t when it’s a generic type.

Therefore I would like to fetch the properties using Reflection like this:

PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public);

and somehow combind that with the above Lambda-expression so that it searches All the public properties of the Type and see if it contains “Filip”, at this time I do not care if the property-name is Name or not.

Is this possible?

  • 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-13T13:12:03+00:00Added an answer on May 13, 2026 at 1:12 pm
    var result = Items.FindAll(x => 
        properties.Any(p => p.PropertyType == typeof(string) && 
                            p.GetValue(x, null) == "Filip"));
    

    Obviously this is a simplistic, optimistic string comparison (you might want to use string.Compare, for example), but this should make the idea clear.

    Edit

    dtb makes a good suggestion in using expression trees. You could accomplish what you’re after in a faster fashion like this:

    public static class PropertyScanner
    {
        static Func<TType, bool> CreatePredicate<TType, TValue>(TValue value, IEqualityComparer<TValue> comparer)
        {
            var arg = Expression.Parameter(typeof(TType), "arg");
    
            Expression body = null;
    
            Expression<Func<TValue, TValue, bool>> compare = (val1, val2) => comparer.Equals(val1, val2);
    
            foreach (PropertyInfo property in typeof(TType).GetProperties(BindingFlags.Public))
            {
                if (property.PropertyType == typeof(TValue) || typeof(TValue).IsAssignableFrom(property.PropertyType))
                {
                    Expression prop = Expression.Equal(Expression.Invoke(compare, new Expression[]
                                           {
                                               Expression.Constant(value),
                                               Expression.Property(arg, property.Name)
                                           }),
                                                   Expression.Constant(0));
    
                    if (body == null)
                    {
                        body = prop;
                    }
                    else
                    {
                        body = Expression.OrElse(body, prop);
                    }
                }
            }
    
            return Expression.Lambda<Func<TType, bool>>(body, arg).Compile();
        }
    
        public static IEnumerable<TType> ScanProperties<TType, TValue>(this IEnumerable<TType> source, TValue value)
        {
            return ScanProperties<TType, TValue>(source, value, EqualityComparer<TValue>.Default);
        }
    
        public static IEnumerable<TType> ScanProperties<TType, TValue>(this IEnumerable<TType> source, TValue value, IEqualityComparer<TValue> comparer)
        {
            return source.Where(CreatePredicate<TType, TValue>(value, comparer));
        }
    }
    

    This will allow you to do something like this:

    var result = Items.ScanProperties("Filip").ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.