根据输入的模型属性表达式获取名称
public T GetEntity<T>(string tableName, Dictionary<Expression<Func<T,object>>,object> conditionDic) where T : new() { var dic = new Dictionary<string, object>(); foreach (var kp in conditionDic) { var columnName = ""; if (kp.Key.Body is UnaryExpression) { columnName = ((MemberExpression)((UnaryExpression)kp.Key.Body).Operand).Member.Name; } else if (kp.Key.Body is MemberExpression) { columnName = ((MemberExpression)kp.Key.Body).Member.Name; } else if (kp.Key.Body is ParameterExpression) { columnName = ((ParameterExpression)kp.Key.Body).Type.Name; } if (string.IsNullOrEmpty(columnName)) continue; dic.Add(columnName, key.Value); } //return GetEntity<T>(tableName, dic); }
使用方式:
var dic = new Dictionary<Expression<Func<Entity,object>>, object> { {p => p.Member1, "test1"}, {p => p.Member2, 2} }; var tmp = GetEntity("TableName", dic);