Is there a way to use `dynamic` in lambda expression tree?

后端 未结 3 918
感动是毒
感动是毒 2021-01-17 10:16

First, spec. We use MVC5, .NET 4.5.1, and Entity framework 6.1.

In our MVC5 business application we have a lot of repetitive CRUD code. My job is to \"automate\" mos

3条回答
  •  爱一瞬间的悲伤
    2021-01-17 10:47

    Take a look at this answer. It uses reflection to get the ID Property. I think it solves your problem:

    public static object GetPropValue(object src, string propName)
    {
        return src.GetType().GetProperty(propName).GetValue(src, null);
    }
    

    Then you replace your lambda expression by

    return entity => GetPropValue(entity, "ID");
    

    I've not tested, since I have no code fully working to test it. If it works please let us know.

提交回复
热议问题