Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq expression IEnumerable<TEntity> does not contain definition of where

How to write correct Linq expression used in generic for condition "where"

public static class ConStr
{
    public static MySqlConnection Conn()
    {
        return new MySqlConnection(ConfigurationManager.ConnectionStrings["DBCN"].ConnectionString);
    }

}

Repositor.cs

private IDbConnection cn;

public IEnumerable<TEntity> FilterBy(Expression<Func<TEntity, bool>> expression)
{
     using(cn = ConStr.Conn())
     {
        return cn.GetAll<TEntity>(null).Where(expression); <--error does not contain definition of where
     }

 }

but this with Linq expression will run

using (IDbConnection cn = ConStr.Conn()) 
{
    var que = cn.GetAll<Cause>(null).Where(x=>x.cause_id == 1);            
    bool dbIE = Utils.IsAny<Cause>(que);
    if (dbIE == true)
    {
        DGRID.DataSource = que;
    }
    else 
    {
        MessageBox.Show("Sorry No Value");
    }
}  
like image 338
jake talledo Avatar asked Apr 23 '16 11:04

jake talledo


1 Answers

Where for IEnumerable<T> does not contains overload that takes Expression. To use Where with Expression you must change the result of GetAll to IQueryable. For your particular case you can just change Expression<Func<TEntity, bool>> expression to Func<TEntity, bool> expression and everything should work.

like image 123
Maxim Kosov Avatar answered Nov 02 '22 18:11

Maxim Kosov