Click here to Skip to main content
15,921,716 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How I can use MemberExpression with dynamic column LINQ sql

C#
Exam_Entity _db = new Exam_Entity();
            var parameter = Expression.Parameter(typeof(Exams_GAT_SAT), "g");
            var member = Expression.Property(parameter, "SAT_av"); 
            
            var query = (from g in _db.exams_GAT_SAT where member != null select g).ToList();


When compiler it gives me the following error
Unable to create a constant value of type 'System.Linq.Expressions.MemberExpression'. Only primitive types or enumeration types are supported in this context


What I have tried:

public static Expression<Func<T, bool>> getExpression<T>(string columnName)
{
    //     var parameter = Expression.Parameter(typeof(Exams_GAT_SAT), "g"); // g =>
    //    var left = Expression.PropertyOrField(parameter, "ayear");
    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);
}
public static Expression<Func<TItem, bool>> PropertyEquals<TItem, TValue>(PropertyInfo property, TValue value)
{
    var param = Expression.Parameter(typeof(TItem));
    var body = Expression.Equal(Expression.Property(param, property),
        Expression.Constant(value));
    return Expression.Lambda<Func<TItem, bool>>(body, param);
}
Posted
Updated 6-Oct-22 11:52am

You haven't explained what you're actually trying to do. At a guess, you want something like:
C#
var parameter = Expression.Parameter(typeof(Exams_GAT_SAT), "g");
var member = Expression.Property(parameter, "SAT_av");
var value = Expression.Constant(null, member.Type);
var body = Expression.NotEqual(member, value);
var predicate = Expression.Lambda<Func<Exams_GAT_SAT, bool>>(body, parameter);
// predicate: g => g.SAT_av != null

var query = _db.exams_GAT_SAT.Where(predicate).ToList();
 
Share this answer
 
Thank you so much
very Wonderful
I'm starting to get it
Now can I use it IN join LINQ sql same this
Exam_Entity _db = new Exam_Entity();
var task = ((from a in _db.AdminUsers
  join g in _db.exams_GAT_SAT
  on a.user_id equals g.sch_id.ToString()
  where a.user_type == 3 && g.ayear == 1442
  //      && (body)
  orderby g.SAT_av descending
  select new
  {
      user_id = a.user_id,
      user_name = a.user_name,
      SAT_av = g.SAT_av,
      GAT_S_av = g.GAT_S_av,
      GAT_T_av = g.GAT_T_av,
      SAT_or = g.SAT_or,
      GAT_S_or = g.GAT_S_or,
      GAT_T_or = g.GAT_T_or,
      HSC_av = g.HSC_av,
  }).AsEnumerable().Select(x => new Exams_Trans_GaT_SaS
  {
      user_id = x.user_id,
      user_name = x.user_name,
      SAT_av = x.SAT_av,
      GAT_S_av = x.GAT_S_av,
      GAT_T_av = x.GAT_T_av,
      SAT_or = x.SAT_or,
      GAT_S_or = x.GAT_S_or,
      GAT_T_or = x.GAT_T_or,
      HSC_av = x.HSC_av,
  }).ToList());
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900