I have a number of calls for the same query but with slightly different WHERE
clause, does anyone know if it's possible to pass a variable as the column name as I can't seem to acheive it.
I know the below isn't correct but just to give you an idea of what i'm trying to acheive.
public EmailUserListViewModel EmailUserListData(int CaseId, string ColumnName)
{
CaseId = CaseId,
EmailUserList = (from u in efContext.PgsUsers
where ColumnName == true
orderby u.FirstName, u.LastName
select new EmailUserListModel
{
UserId = u.Id,
Name = ((u.FirstName == null) ? "" : u.FirstName)
+ ((u.LastName == null) ? "" : " " + u.LastName),
Email = u.Email,
Checked = false
}).ToList()
};
}
I suppose you could use Reflection to dynamically retrieve the value of the property
from u in efContext.PgsUsers where (typeof(PgsUser).GetProperty(ColumnName).GetValue(u) as bool) == true
or
from u in efContext.PgsUsers where (u.GetType().GetProperty(ColumnName).GetValue(u) as bool) == true
You could write such method:
public Expression<Func<T, bool>> getExpression<T>(string columnName)
{
var param = Expression.Parameter(typeof(T));
var equal = Expression.Equal(Expression.Property(param, columnName), Expression.Constant(true));
return (Expression<Func<T, bool>>)Expression.Lambda(equal, param);
}
and use it in where:
where getExpression<YourType>("ColumnName")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.