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

时间:2016-04-23 11:25:31

标签: c# linq generics generic-programming

如何在条件&#34中使用通用的正确Linq表达式,其中&#34;

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
     }

 }

但使用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");
    }
}  

1 个答案:

答案 0 :(得分:4)

Where的{​​p> IEnumerable<T>不包含Expression的重载。要将WhereExpression一起使用,您必须将GetAll的结果更改为IQueryable。对于您的特定情况,您只需将Expression<Func<TEntity, bool>> expression更改为Func<TEntity, bool> expression,一切都应该有效。