转换为C#表达式树

时间:2019-09-21 15:15:56

标签: c# entity-framework lambda expression

我正在使用c#'s expression tree并尝试为我们的entity framework查询创建动态选择。我现在仍处于实验阶段。

有人可以帮助我将此语句转换为表达式树吗?

  Expression<Func<Person, Person>> ex = x => new Person
            {
                Id = x.Id,
                FirstName = x.FirstName,
                LastName = x.LastName,
                Manager = new Person()
                {
                    Id = x.Id,
                    FirstName = x.Manager.FirstName,
                    LastName = x.Manager.LastName
                },
                Subordinates = x.Subordinates.Select(s => new Person()
                {
                    Id = s.Id,
                    FirstName = s.FirstName,
                    LastName = s.LastName
                }).ToList()
            };

除“下属列表”外,我已经实现了大多数。

var param = Expression.Parameter(typeof(Person), "x");

            var createdType = typeof(Person);

            var personObject = Expression.New(createdType);

            var idValue = Expression.PropertyOrField(param, "Id");
            var idProperty = createdType.GetProperty("Id");
            var idAssignment = Expression.Bind(idProperty, idValue);

            var FirstNameValue = Expression.PropertyOrField(param, "FirstName");
            var FirstNameProperty = createdType.GetProperty("FirstName");
            var FirstNameAssignment = Expression.Bind(FirstNameProperty, FirstNameValue);

            var LastNameValue = Expression.PropertyOrField(param, "LastName");
            var LastNameProperty = createdType.GetProperty("LastName");
            var LastNameAssignment = Expression.Bind(LastNameProperty, LastNameValue);

            /* + manager object */
            var managerObject = Expression.New(typeof(Person));
            var managerIdValue = Expression.PropertyOrField(param, "Id");
            var managerIdProperty = createdType.GetProperty("Id");
            var managerIdAssignment = Expression.Bind(managerIdProperty, managerIdValue);

            var managerFirstNameValue = Expression.PropertyOrField(param, "FirstName");
            var managerFirstNameProperty = createdType.GetProperty("FirstName");
            var managerFirstNameAssignment = Expression.Bind(managerFirstNameProperty, managerFirstNameValue);

            var managerLastNameValue = Expression.PropertyOrField(param, "LastName");
            var managerLastNameProperty = createdType.GetProperty("LastName");
            var managerLastNameAssignment = Expression.Bind(managerLastNameProperty, managerLastNameValue);

            var managerMemberAssignments = new List<MemberAssignment>()
            {
                managerIdAssignment,
                managerFirstNameAssignment,
                managerLastNameAssignment
            };
            var managerObjectInit = Expression.MemberInit(managerObject, managerMemberAssignments);

            var managerProperty = createdType.GetProperty("Manager");
            var managerAssignment = Expression.Bind(managerProperty, managerObjectInit);
            /* - manager object */

            /* + subordinates list */
            var s = Expression.Parameter(typeof(Person), "s");
            var subordinateObject = Expression.New(typeof(Person));

            var subordinateIdValue = Expression.PropertyOrField(s, "Id");
            var subordinateIdProperty = createdType.GetProperty("Id");
            var subordinateIdAssignment = Expression.Bind(subordinateIdProperty, subordinateIdValue);

            var subordinateFirstNameValue = Expression.PropertyOrField(s, "FirstName");
            var subordinateFirstNameProperty = createdType.GetProperty("FirstName");
            var subordinateFirstNameAssignment = Expression.Bind(subordinateFirstNameProperty, subordinateFirstNameValue);

            var subordinateLastNameValue = Expression.PropertyOrField(s, "LastName");
            var subordinateLastNameProperty = createdType.GetProperty("LastName");
            var subordinateLastNameAssignment = Expression.Bind(subordinateLastNameProperty, subordinateLastNameValue);

            var subordinateListMemberAssignments = new List<MemberAssignment>()
            {
                subordinateIdAssignment,
                subordinateFirstNameAssignment,
                subordinateLastNameAssignment
            };

            var subordinateObjectInit = Expression.MemberInit(subordinateObject, subordinateListMemberAssignments);
            var subordinateLambda = Expression.Lambda<Func<Person, Person>>(subordinateObjectInit, s);


            var subordinatesValue = Expression.PropertyOrField(param, "Subordinates");
            var subordinatesSelect = Expression.Call()
            var subordinatesProperty = createdType.GetProperty("Subordinates");
            var subordinatesAssignment = Expression.Bind(subordinatesProperty, subordinateObjectInit);


            /* - subordinates list */

            var personMemberAssignments = new List<MemberAssignment>()
            {
                idAssignment,
                FirstNameAssignment,
                LastNameAssignment,
                managerAssignment,
                subordinatesAssignment
            };
            var personInit = Expression.MemberInit(personObject, personMemberAssignments);


            var personLambda = Expression.Lambda<Func<Person, Person>>(personInit, param);

            var result = await _applicationDbContext.Persons.Select(personLambda).ToListAsync();

我的问题在这里。

var subordinatesValue = Expression.PropertyOrField(param, "Subordinates");
            var subordinatesSelect = Expression.Call()
            var subordinatesProperty = createdType.GetProperty("Subordinates");
            var subordinatesAssignment = Expression.Bind(subordinatesProperty, subordinateObjectInit);

我不知道如何将其翻译为表达式。

Subordinates = x.Subordinates.Select(s => new Person()
                {
                    Id = s.Id,
                    FirstName = s.FirstName,
                    LastName = s.LastName
                }).ToList()

这已经是Person Object的子属性

0 个答案:

没有答案