How to write correct Linq expression used in generic for condition "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
}
}
but this with Linq expression will run
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>
does not contains overload that takes Expression
. To use Where
with Expression
you must change the result of GetAll
to IQueryable
. For your particular case you can just change Expression<Func<TEntity, bool>> expression
to Func<TEntity, bool> expression
and everything should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With