How do I reuse an Expression on a single object in another Expression

后端 未结 2 428
清歌不尽
清歌不尽 2021-01-23 06:42

I feel like I am missing something simple, but I have not found the documentation that answers my question.

I have recently been decomposing some of the linq projection

2条回答
  •  太阳男子
    2021-01-23 07:15

    You can:

    public static Expression> CreateDepartmentDto = d =>
         new DepartmentDto
            {
                Manager = CreatePersonDto.Compile()(d.Manager),
                Employees = d.Employees.Select(CreatePersonDto.Compile()).ToList() // Or declare Employees as IEnumerable and avoid ToList conversion
            };
    

    Clearly, instead of calling two times Compile method you can store the compiled Func

提交回复
热议问题