Johan
Johan

Reputation: 35194

Reference object property as a method parameter

This won't work, but should give you an idea of what I'm trying to acheive:

public static IEnumerable<T> MyMethod<T>(this IEnumerable<T> entity, 
                               string param, string SomeProp)
{
    return entity.Where(l =>
    System.Data.Objects.SqlClient.SqlFunctions.PatIndex(param, l.SomeProp) > 0);
}

Do I have to pass the entire Where() parameter as a function to MyMethod?

Upvotes: 3

Views: 93

Answers (1)

recursive
recursive

Reputation: 86064

Seeing your update, you can avoid repetition like this:

public static IEnumerable<T> MyMethod<T>(this IEnumerable<T> entity, 
                               string param, Func<T, string> selector)
{
    return entity.Where(l =>
    System.Data.Objects.SqlClient.SqlFunctions.PatIndex(param, selector(l)) > 0);
}

Upvotes: 3

Related Questions