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

标签 c# linq generics generic-programming

如何编写用于条件“where”的泛型的正确 Linq 表达式

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

最佳答案

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

关于c# - Linq 表达式 IEnumerable<TEntity> 不包含 where 的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36810185/

相关文章:

c# - LINQ to Entities 对具有嵌套对象的 Union 的空引用

c# - Rx CatchAll 用于未观察到的消息

generics - (FS0193、FS1113)具有静态解析类型参数的 F# 向量类

java - 参数化类 'ABC' 的原始使用

c# - 在列表中查找连续的重复项

c# - Windows 窗体/ TreeView : How do I put CheckBoxes only on the leaves?

C#:如何使用 LINQ 或 LAMBDA 表达式操作 List<String>

java - 使用扩展类的泛型

c# - TCP 在不关闭连接的情况下发送多条消息

c# - Regex.IsMatch 不应该返回 true