Query using LINQ and Generics C # [closed]

1

I'm having trouble running a LINQ query using Generics.

Something like:

from x in session.Query<T>() " ??? "

My method receives two strings as parameters, columnName and search value.

public virtual object PesquisePorColunaValor(string colunaDeBusca, string valorDeBusca)
    {
         // TODO Query em LINQ
    }
    
asked by anonymous 22.09.2015 / 20:42

1 answer

3

I believe you can not do this dynamic search using a String to get the column to search.

Ideally, you should use Expression , Func and Generics .

public virtual IQueryable<T> Pesquisar(Expression<Func<T, bool>> predicate)
{
    IQueryable<T> lquerySentenca = session.Query<T>().Where(predicate);
    return lquerySentenca;
}

public virtual List<T> PesquisarComList(Expression<Func<T, bool>> predicate)
{
    return Pesquisar(predicate).ToList();
}

public void Executando()
{
    List<Aluno> lst = Pesquisar<Aluno>(o => o.Nome == "Jose").ToList();
    List<Aluno> lst2 = PesquisarComList<Aluno>(o => o.Nome == "Jose").ToList();
}
    
22.09.2015 / 21:17