3

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?

0

1 Answer 1

3

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

return return won't compile.
Followup question: Is there any way to pass multiple properties? For example, I want to get ...PatIndex(param, l.SomeProp) > 0 && PatIndex(param, l.SomeOtherProp) > 0... How would I pass that as the function?
@Johan: You could achieve that by chaining calls. i.e. collection.MyMethod("p1", t => t.Prop1).MyMethod("p2", t => t.Prop2)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.