jak*_*edo 5 c# linq generics generic-programming
如何在通用条件"where"中编写正确的Linq表达式
public static class ConStr
{
public static MySqlConnection Conn()
{
return new MySqlConnection(ConfigurationManager.ConnectionStrings["DBCN"].ConnectionString);
}
}
Run Code Online (Sandbox Code Playgroud)
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
}
}
Run Code Online (Sandbox Code Playgroud)
但这与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");
}
}
Run Code Online (Sandbox Code Playgroud)
WhereforIEnumerable<T>不包含需要 的重载Expression。要Where与Expression您一起使用,必须将结果更改GetAll为IQueryable。对于您的特定情况,您可以更改Expression<Func<TEntity, bool>> expression为Func<TEntity, bool> expression,一切都应该有效。
| 归档时间: |
|
| 查看次数: |
448 次 |
| 最近记录: |