开发者

How do I dynamically create an Lambda Expression

How to I crate Expression Tree dynamically that looks somet开发者_运维知识库hing like...

    p => new { p.FAMILY, p.CACHE_FAMILY, p.CUSTOMER_CODE, p.CCC, p.OPERATION, p.EVAL_CODE, p.VDT_FLAG, p.TEST_PLATFORM, p.PCBA_VENDOR }

currently my code can only handle for single expression.

public static void MapKeys<TEntity>(EntityTypeConfiguration<TEntity> mapper, string tableName)
        where TEntity : class
    {
        Type type = typeof(TEntity);
        ParameterExpression arg = Expression.Parameter(type, "x");
        Expression expr = arg;


        string propertyName = string.Empty;
        List<string> indexKeys = GetIndexKeyList(tableName);

        foreach (string key in indexKeys)
        {
            PropertyInfo pi = type.GetProperty(key,
            BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
            expr = Expression.Property(expr, pi);


            LambdaExpression lambda = Expression.Lambda(expr, arg);

            Expression<Func<TEntity, string>> expression = (Expression<Func<TEntity, string>>)lambda;
            mapper.HasKey(expression);
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜