Linq表达式IEnumerable <TEntity>不包含where的定义

jak*_*edo 5 c# linq generics generic-programming

如何在通用条件"where"中编写正确的Linq表达式

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

}
Run Code Online (Sandbox Code Playgroud)

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
     }

 }
Run Code Online (Sandbox Code Playgroud)

但这与Linq表达式将运行

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");
    }
}  
Run Code Online (Sandbox Code Playgroud)

Max*_*sov 4

WhereforIEnumerable<T>不包含需要 的重载Expression。要WhereExpression您一起使用,必须将结果更改GetAllIQueryable。对于您的特定情况,您可以更改Expression<Func<TEntity, bool>> expressionFunc<TEntity, bool> expression,一切都应该有效。