如何编写用于条件“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
的重载.使用 Where
与 Expression
您必须更改 GetAll
的结果至 IQueryable
.对于您的特定情况,您可以更改 Expression<Func<TEntity, bool>> expression
至 Func<TEntity, bool> expression
一切都应该有效。
关于c# - Linq 表达式 IEnumerable<TEntity> 不包含 where 的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36810185/