从对象构建表达式 [英] Build expression from object

查看:72
本文介绍了从对象构建表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从ASP.NET Core中的Angular客户端应用程序收到以下对象:

I have the following object received from Angular client application in ASP.NET Core:

public class ModelFromClient
{
  public string name {get;set;}      //Database field is Name
  public int qunatity {get;set;}     //Database field is Quantity
}

我有一个EF表类:

[Table("MyTable")]
public class MyRow
{
  public int Id {get;set;}  
  public string Name {get;set;}
  public int Qunatity {get;set;}
}

现在,我需要创建从ModelFromClientExpression<Func<MyRow, MyRow>>的表达式,并且需要泛型表达式. 没有泛型的解决方案将是:

Now I need to create expression from ModelFromClient to Expression<Func<MyRow, MyRow>> and I need it with generic. Without generics solution would be:

public Expression<Func<MyRow, MyRow>> ToExpression(ModelFromClient Model)
{
    Expression<Func<MyRow, MyRow>> result = (t) => new MyRow()
    {
        Name = Model.name, 
        Quantity = Model.qunatity
    };
    return result;
}

但是我想要这样的东西:

But I would like something like that:

public Expression<Func<T, T>> ToExpression<T>(object Model) where T: new()
{
    Expression<Func<T, T>> result = (t) => new T();
    foreach(var prop in Model.GetType().GetProperties()) 
    {
       //compile error Fields does not exists.
       result.Body.Fields.Add(prop.Name.Capitalize(), prop.GetValue(Model, null));  //capitalize returns Name from input name
    }
    return result;
}

我需要表达式将其传递给EntityFramework-的更新扩展方法加.

I need expression to pass it to Update extension method of EntityFramework-Plus.

推荐答案

免责声明:我是该项目的所有者实体框架增强版

Disclaimer: I'm the owner of the project Entity Framework Plus

以下是帮助您入门的小提琴: https://dotnetfiddle.net/JY0wzw

Here is a fiddle to get you started: https://dotnetfiddle.net/JY0wzw

using System;
using System.Collections.Generic;
using System.Linq.Expressions;

public class Program
{
    public class MyRow
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Qunatity { get; set; }
    }

    public static void Main()
    {
        var type = typeof(MyRow);
        var constructorInfo = type.GetConstructor(new Type[0]);
        var newExpression = Expression.New(constructorInfo);

        var memberInits = new List<MemberAssignment>();
        foreach (var prop in type.GetProperties())
        {
            if (prop.Name == "Id")
            {
                memberInits.Add(Expression.Bind(prop, Expression.Constant(1)));
            }
            else if (prop.Name == "Name")
            {
                memberInits.Add(Expression.Bind(prop, Expression.Constant("Z_Name")));
            }
            else if (prop.Name == "Qunatity")
            {
                memberInits.Add(Expression.Bind(prop, Expression.Constant(2)));
            }
        }

        var expression = Expression.MemberInit(newExpression, memberInits);

        // FOR testing purpose
        var compiledExpression = Expression.Lambda<Func<MyRow>>(expression).Compile();
        var myRow = compiledExpression();

        Console.WriteLine(myRow.Id);
        Console.WriteLine(myRow.Name);
        Console.WriteLine(myRow.Qunatity);
    }
}

免责声明:我是该项目的所有者 Eval-Expression.NET

Disclaimer: I'm the owner of the project Eval-Expression.NET

该库不是免费的,但允许您在运行时动态创建代码.与以前的解决方案相比,熟悉后,几乎可以轻松完成任何您想做的事情.

This library is not free but allows you to create code dynamically at runtime. Once you get familiar, you can do pretty much anything you want way more easily than with the previous solution.

// Register your type
EvalManager.DefaultContext.RegisterType(typeof(MyRow));

// Register extension methods once from Z.EntityFramework.Plus
EvalManager.DefaultContext.RegisterExtensionMethod(typeof(BatchUpdate));

Eval.Execute("query.Update(x => new MyRow() { Id = 1, Name = 'Z_Name', Qunatity = 2});", new {query});

这篇关于从对象构建表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆