Linq 表达式 IEnumerable<TEntity> 不包含 where 的定义
Linq expression IEnumerable<TEntity> does not contain definition of where
如何编写用于条件泛型的正确 Linq 表达式 "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
}
}
但是这个 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
for IEnumerable<T>
不包含采用 Expression
的重载。要将 Where
与 Expression
一起使用,您必须将 GetAll
的结果更改为 IQueryable
。对于您的特定情况,您只需将 Expression<Func<TEntity, bool>> expression
更改为 Func<TEntity, bool> expression
,一切都应该有效。
如何编写用于条件泛型的正确 Linq 表达式 "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
}
}
但是这个 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
for IEnumerable<T>
不包含采用 Expression
的重载。要将 Where
与 Expression
一起使用,您必须将 GetAll
的结果更改为 IQueryable
。对于您的特定情况,您只需将 Expression<Func<TEntity, bool>> expression
更改为 Func<TEntity, bool> expression
,一切都应该有效。