c# - Variable condition in LINQ where clause -


i have database did not create , cannot modify. need run linq query, need pass in variable in where clause.

columns in table: advmonam (all bit), advmonam, advtueam, advtuepm, advwedam, etc.

var column = "adv" + dayofweek + time; var employeesoncall = r in db.advoncalls                       (variable column needed here) == true                       select r.chartemployee; 

if hard code r.advtueam works perfectly, r.column == true or column == true not. feel should easy, stumped.

i trying find employee(s) on call @ given time of day.

you can build expression tree represent condition want manually, rather using lambda you're trying do:

public static iqueryable<t> whereequals<t>(     iqueryable<t> query,     string property,     object valuetocompare) {     var param = expression.parameter(typeof(t));     var body = expression.equal(         expression.property(param, property),         expression.constant(valuetocompare));     var lambda = expression.lambda<func<t, bool>>(body, param);     return query.where(lambda); } 

this allows write:

var employeesoncall = db.advoncalls.whereequals(column, true)     .select(adv => adv.chartemployee); 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -