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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:41:00+00:00 2026-05-14T15:41:00+00:00

I have the following extension method public static class ListExtensions { public static IEnumerable<T>

  • 0

I have the following extension method

public static class ListExtensions
    {

        public static IEnumerable<T> Search<T>(this ICollection<T> collection, string stringToSearch)
        {
            foreach (T t in collection)
            {
                Type k = t.GetType();
                PropertyInfo pi = k.GetProperty("Name");
                if (pi.GetValue(t, null).Equals(stringToSearch))
                {
                    yield return t;
                }
            }
        } 

    }

What it does is by using reflection, it finds the name property and then filteres the record from the collection based on the matching string.

This method is being called as

List<FactorClass> listFC = new List<FactorClass>();
    listFC.Add(new FactorClass { Name = "BKP", FactorValue="Book to price",IsGlobal =false  });
    listFC.Add(new FactorClass { Name = "YLD", FactorValue = "Dividend yield", IsGlobal = false });
    listFC.Add(new FactorClass { Name = "EPM", FactorValue = "emp", IsGlobal = false });
    listFC.Add(new FactorClass { Name = "SE", FactorValue = "something else", IsGlobal = false });    
   List<FactorClass> listFC1 = listFC.Search("BKP").ToList();

It is working fine.

But a closer look into the extension method will reveal that

Type k = t.GetType();
PropertyInfo pi = k.GetProperty("Name");

is actually inside a foreach loop which is actually not needed. I think we can take it outside the loop.

But how?

PLease help. (C#3.0)

  • 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-14T15:41:01+00:00Added an answer on May 14, 2026 at 3:41 pm

    There’s a couple of things you could do — first you could institute a constraint on the generic type to an interface that has a name property. If it can only take a FactorClass, then you don’t really need a generic type — you could make it an extension to an ICollection<FactorClass>. If you go the interface route (or with the non-generic version), you can simply reference the property and won’t have a need for reflection. If, for some reason, this doesn’t work you can do:

     var k = typeof(T);
     var pi = k.GetProperty("Name");
     foreach (T t in collection)  
     {   
          if (pi.GetValue(t, null).Equals(stringToSearch))  
          {  
               yield return t;  
          }
     }  
    

    using an interface it might look like

     public static IEnumerable<T> Search<T>(this ICollection<T> collection, string stringToSearch) where T : INameable
     {   
        foreach (T t in collection)   
        {      
            if (string.Equals( t.Name, stringToSearch))   
            {   
                yield return t;   
            }   
        }
     }
    

    EDIT: After seeing @Jeff’s comment, this is really only useful if you’re doing something more complex than simply checking the value against one of the properties. He’s absolutely correct in that using Where is a better solution for that problem.

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

Sidebar

Related Questions

I have the following extension method: public static void ThrowIfArgumentIsNull<T>(this T value, string argument)
I have the following method: public static string UlList(this HtmlHelper helper, List<IEntity> entities, string
I have an extension method defined like so: public static TSource MaxBy<TSource, TResult>(this IEnumerable<TSource>
I have an extension method with the following signature (in BuildServerExtensions class):: public static
Let's say I have the following method: public static int CountNonNullMembers<T>(this IEnumerable<T> enumerable) {
I have the following class and extension class (for this example): public class Person<T>
This extension method does not work on two separate development machines: public static string
I have the following method: public static TEventInvocatorParameters Until <TEventInvocatorParameters, TEventArgs>(this TEventInvocatorParameters p, Func<TEventArgs,
I have a class CURPRFC that contains the following: static public string CalcRFC(string firstname,
Why does this happen? Please observe the following code: static class StringExtension { public

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.