C# Linq表达式IEnumerable<;张力>;不包含where的定义

C# Linq表达式IEnumerable<;张力>;不包含where的定义,c#,linq,generics,generic-programming,C#,Linq,Generics,Generic Programming,如何编写泛型条件“where”中使用的正确Linq表达式 Repositor.cs private IDbConnection cn; public IEnumerable<TEntity> FilterBy(Expression<Func<TEntity, bool>> expression) { using(cn = ConStr.Conn()) { return cn.GetAll<TEntity>(nu

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

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
     }

 }
idbcn私有连接;
公共IEnumerable筛选器by(表达式)
{
使用(cn=cont.Conn())
{
返回cn.GetAll(null)。其中(表达式);x.cause\u id==1);
bool dbIE=Utils.IsAny(que);
if(dbIE==true)
{
DGRID.DataSource=que;
}
其他的
{
MessageBox.Show(“对不起,没有值”);
}
}  

其中
对于
IEnumerable
不包含使用
表达式的重载。要将
中的
表达式一起使用
必须将
GetAll
的结果更改为
IQueryable
。对于特定情况,您只需将
表达式表达式
更改为
函数即可xpression
一切都应该正常。

最有可能的是,您的
GetAll
返回的
IEnumerable
没有
Where
Expression
,但是
Func
。从视觉上看,隐式
Expression
Func
之间没有区别,这就是第二个代码段编译的原因es。不实现IEnumerable,您可以将所有项放在列表集合中。列表集合是IEnumerable,因此您不必编写自己的实现。@IvanStoev啊,我明白了,非常感谢,我将尝试更改注意,如果在返回该列表时连接已关闭,则您的
FilterAll
方法很可能无法工作
IQueryable
实现。@casperOne这取决于
GetAll
扩展如何实现hanks so Func expression->用于IEnumerable,for is用于IQueryable is->expression expression。还有
AsQueryable()
IEnumerable
类型的扩展,它将您的
IEnumerable
转换为
IQueryable
,但我真的不知道您为什么要在生产中使用它。大多数情况下,简单的
Func
Action
就足够了,如果您想使用
Expression
,您需要知道为什么
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
     }

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