从传递的lambda表达式的类型和属性生成Linq查询

时间:2016-10-03 21:32:00

标签: entity-framework lambda

    int getMaxValue<SomeEntity>(x => x.SomePropertyOfSomeEntity)

    int getMaxValue<SomeEntity>(Expression<Func<SomeEntity, int>> SomePropertyOfSomeEntity)
    {
        var memberExpression = (MemberExpression)SomePropertyOfSomeEntity.Body;
        var propertyInfo = (PropertyInfo)memberExpression.Member;

        DBContext db = new DBContext();


        //somehow convert property info and SomeEntity into this query, using db for the context

        int maxValueOfProperty = (from x in db.SomeEntity
                                  select x.SomePropertyOfSomeEntity).Max();
        return maxValueOfProperty;
    }

0 个答案:

没有答案