Use Where Enclosure when class is dynamic

Asked

Viewed 29 times

2

I have a base class:

public class MinhaClasse<T> : Controller where T : class
{
    public List<T> BuscarVarios(string nome)
    {
        return db.Set<T>().Where(/*?????*/);
    }
}

How do I use the cloister Where when my class is dynamic?

  • 2

    Welcome to [en.so]. Your question is very vague. Please click on [Edit] and put more details so we can help you.

  • It’s true @jbueno .

1 answer

2


Although I can’t see a reason to do so. The most I can think of right now to solve this problem is to ask for a Func<T, bool> instead of a string.

See in practice

public class MinhaClasse<T> : Controller where T : class
{
    public List<T> BuscarVarios(Func<T, bool> filtro)
    {
        return db.Set<T>().Where(filtro).ToList();
    }
}

When using you would do

minhaClassePersonalizada.BuscarVarios(x => x.Nome == "Joaquim");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.