2

Say I have a class that looks something like this:

class SomeClass {
    int m_member;

    public int Member {
        get { return m_member; }
        set { m_member = value; }
    }
}

And somewhere else, I have a list of type List<SomeClass> list.

If I want to search the list for a particular instance of the class, I can just do

int index = list.IndexOf(someInstance);

But if I want to search the list by Member, I have to do this:

int index = -1;
for (int i = 0; i < list.Count; i++) {
    if (list[i].Member == someMember) {
        index = i;
        break;
    }
}

Is there a better way to do this?

3 Answers 3

7
int index = list.FindIndex(m => m.Member == someMember);
2
  • 2
    If you need the item instead of the index, you can use Find() instead of FindIndex() (Not what the OP asked for, just an FYI). Commented Nov 12, 2009 at 2:47
  • Thanks 280Z28, that comes in useful elsewhere.
    – Matthew
    Commented Nov 12, 2009 at 3:28
3

If you can use Linq

SomeClass aClasss = list.FirstOrDefault(item => item.Member == someMember);
1
  • 2
    If you're using List<T>, this isn't what you want, but it does work for finding an item in an enumeration - note that it doesn't find the index of the item though. Commented Nov 12, 2009 at 2:46
1

You can have an extended search method(using reflection) for the list like below where property name and search value are input parameters.

public static List<T> SearchList<T>(this List<T> list, string PropertyName, string SearchValue)
{
    return list.Select(item =>
        new
        {
            i = item,
            Props = item.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)
        })
        .Where(item => item.Props.Any(p =>
        {
            var val = p.GetValue(item.i, null);
            return val != null
                && (p.Name.ToLower() == PropertyName.ToLower() || string.IsNullOrEmpty(PropertyName))
                && (val.ToString().ToLower().Contains(SearchValue.ToLower()) || string.IsNullOrEmpty(SearchValue));
        }))
        .Select(item => item.i)
        .ToList();
    }

calling:

List<Employee> employees = new List<Employee>();
var searchedEmployees = data.SortList(serachProperty, searchValue);
2
  • Thanks for the answer, Buddha. I think you can make this less messy/slow with a few suggestions: 1. Normalize using ToLowerInvariant instead of ToLower; 2. Normalize PropertyName and SearchValue once at the beginning of the method, rather than in lambdas that will be invoked in nested loops; 3. Check string.IsNullOrEmpty before referencing the string, not after; 4. There is no need for either Select clause, suggest to remove them and get the properties in the Where clause instead.
    – Matthew
    Commented May 18, 2016 at 2:06
  • 2
    For anyone stumbling upon this answer: Consider whether using reflection is really the right choice for your scenario. Using reflection is slow, adds unnecessary complexity, and loses most of the benefits of type safety. Consider designing a solution tailored to your specific filtering scenario instead.
    – Matthew
    Commented May 18, 2016 at 2:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.