Nested Expression method call in Linq.Select()

c# entity-framework-core expression lambda linq

Question

I use .Select(i=> new T{...}) after every db hit manually to convert my entity objects into DTO object. Here are some sample entities and DTOS

User Entity;

public partial class User
{
    public int Id { get; set; }
    public string Username { get; set; }
    public virtual UserEx UserEx { get; set; }
}

User DTO;

public class UserDTO
{
    public int Id { get; set; }
    public string Username { get; set; }
}

UserEx entity;

public class UserEx
{
    public int UserId { get; set; }
    public string MyProperty1 { get; set; }
    public virtual User User { get; set; }
}

UserEx DTO;

public class UserExDTO
{
    public int MyProperty1 { get; set; }
    public UserDTO UserModel { get; set; }
}

My Conversion Expression Methods;

public static class ConversionExpression
{
    public static Expression<Func<UserEx, UserExDTO>> GetUserExDTOConversionExpression()
    {
        return userEx => new UserExDTO
        {
            MyProperty1 = userEx.MyProperty1,
            UserModel = new UserDTO
            {
                Id = userEx.User.Id,
                Username = userEx.User.Username
            }
        };
    }

    public static Expression<Func<User, UserDTO>> GetUserDTOConversionExpression()
    {
        return user => new UserDTO
        {
            Id = user.Id,
            Username = user.Username
        };
    }
}

And my current usage for UserDTO;

myContext.Users
    .Select(ConversionExpression.GetUserDTOConversionExpression())
    .ToList();

for UserExDTO;

myContext.UserExes
    .Select(ConversionExpression.GetUserExDTOConversionExpression())
    .ToList();

Apologize for long introduction, now here is my question ; I need to group

new UserDTO
{
    Id = userEx.User.Id,
    Username = userEx.User.Username
}

due to single point of concerns. So I want something like this;

public static Expression<Func<UserEx, UserExDTO>> GetUserExDTOConversionExpression()
{
    return userEx => new UserExDTO
    {
        MyProperty1 = userEx.MyProperty1,
        //this line does not behave like the other one 
        UserModel = userEx.User.GetUserDTOConversionExpression()
    };
}

Is there any way to do that or should I write down every expression individual and nonrelated to similar needs?

1
3
4/27/2018 9:07:22 AM

Accepted Answer

I've solved my issue and beyond only with NeinLinq. Here is my solution; You need to remove nested declarations first.

public static Expression<Func<UserEx, UserExDTO>> GetUserExDTOConversionExpression()
{
    return userEx => new UserExDTO
    {
        MyProperty1 = userEx.MyProperty1
        //We removed other model declaration here.
    };
}

Then use To method of NeinLinq to define the translation;

public static Expression<Func<UserEx, UserExDTO>> GetUserExDtOCompbinedExpression()
    {
    //Translate() and To() methods do all the job
    return GetUserDTOConversionExpression().Translate()
       .To(userEx => userEx.User, userExDTO => userExDTO.UserModel, GetUserExDTOConversionExpression());
    }
2
7/29/2018 2:41:17 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow