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

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -