0

I need to create a method that with a generic enumerable of type T it finds inside it using in the where the term i specify

public static object findInList<T>(T[] list, string searchTerm, string seachIndex)
{
    string normalized1 = Regex.Replace(seachIndex, @"\s", "");
    var sel = (from l in list
                     where normalized1.Equals([the item i want to compare])
                     select l).FirstOrDefault();
    return sel ;
}

i need this because i want to create a generic method for search an item in my array that i can customize in some way (below the code in its original way)

[...]
string normalized1 = Regex.Replace(seachIndex, @"\s", "");
sel = (from l in list
        where normalized1.Equals(l.Ordine)
        select l).FirstOrDefault();
[...]

[edit] Thanks to Servy for the answer. For full index of this answer i add here how to call this method

Func<XXX, string> keySelector = delegate(XXX b) { return b.XX; };
var return = findInList<XXX>(list, keySelector, seachIndex);

Where XXX is the type of the list and XX is the property you want to compare for the search

2
  • 1
    How about adding a 4th parameter where you pass in a delegate? Sep 10, 2014 at 16:19
  • I don't understand what u need Sep 10, 2014 at 16:35

1 Answer 1

2

What you need here is for your method to accept a selector, some function that determines what you should compare for each of your objects.

public static T findInList<T>(
    IEnumerable<T> sequence,
    Func<T, string> keySelector,
    string searchTerm,
    string seachIndex)
{
    string normalized1 = Regex.Replace(seachIndex, @"\s", "");
    return (from l in sequence
            where normalized1.Equals(keySelector(l))
            select l).FirstOrDefault();
}

You can also return a T instead of an object, since you know that that's what it is, ensuring that the caller doesn't need to cast it back to what it is. You can accept an IEnumerable instead of an array since you're only ever iterating it, thus giving the caller more flexibility while still letting you do everything that you need to do.

4
  • How can i call this method with the Func<>? Can you give me an example?
    – theLaw
    Sep 10, 2014 at 16:30
  • 1
    Pass in a method accepting an object of the type in the sequence and returning a string. You can use a lambda if you want, although you don't have to. You can look up the documentation for lambda for plenty of examples.
    – Servy
    Sep 10, 2014 at 16:31
  • @Servy It looks like the OP wants to do a search and replace. In that case you need to use Expression<Func<T, string>> then convert that to a PropertyInfo so that you can get both the get and set of the property, then call get replace then set.
    – Aron
    Sep 10, 2014 at 16:56
  • 2
    @Aron He's only using replace to remove spaces from his search text. It appears he's simply looking for the first item that matches that search text.
    – Servy
    Sep 10, 2014 at 16:58

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.