c# - Build up LINQ expression before querying -
i'm querying data using linq sql, i'd add expressions before submit query. far, have this:
public virtual iqueryable<t> query<t>(expression<func<t, bool>> expression = null) t : class { var table = gettable<t>(); return expression != null ? table.where(expression).orderby("orderby") : table; }
if wanted change expression, or add or orderby, before query actual table? this:
public virtual iqueryable<t> query<t>(expression<func<t, bool>> expression = null) t : class { var table = gettable<t>(); expression.where(my1stwhereclause).where(my2ndwhereclause); expression.orderby("my1stcolumn"); expression.thenby("my2ndcolumn"); return expression != null ? table.where(expression) : table; }
you need use return value each method call, this:
public iqueryable<t> query<t>(expression<func<t, bool>> expression = null) t : class { iqueryable<t> query = gettable<t>(); query = query.where(my1stwhereclause).where(my2ndwhereclause); // doing dynamically doesn't work.. query = expression.orderby(x => x.my1stcolumn) .thenby(x => x.my2ndcolumn); return expression != null ? query.where(expression) : query; }
it's not clear how you're expecting have my1stwhereclause
, my2ndwhereclause
without knowing t
beforehand... , in order call orderby
, thenby
, need know type you're dealing too.
however, idea of building query supported in linq - , when return here, that's still not executed against data source.
Comments
Post a Comment