根據輸入的模型屬性表達式獲取名稱

 

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);
        }

 

使用方式:spa

var dic = new Dictionary<Expression<Func<Entity,object>>, object>
                {
                    {p => p.Member1, "test1"},
                    {p => p.Member2, 2}
                };
var tmp = GetEntity("TableName", dic);
相關文章
相關標籤/搜索