Linq.Select()中的嵌套表达式方法调用 [英] Nested Expression method call in Linq.Select()
问题描述
在每次命中数据库后,我都使用.Select(i=> new T{...})
将我的实体对象转换为DTO对象.这是一些示例实体和DTOS
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
用户实体;
public partial class User
{
public int Id { get; set; }
public string Username { get; set; }
public virtual UserEx UserEx { get; set; }
}
用户DTO;
public class UserDTO
{
public int Id { get; set; }
public string Username { get; set; }
}
UserEx实体;
UserEx entity;
public class UserEx
{
public int UserId { get; set; }
public string MyProperty1 { get; set; }
public virtual User User { get; set; }
}
UserEx DTO;
UserEx DTO;
public class UserExDTO
{
public int MyProperty1 { get; set; }
public UserDTO UserModel { get; set; }
}
我的转化表达方法;
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
};
}
}
以及我目前对UserDTO的使用情况
And my current usage for UserDTO;
myContext.Users
.Select(ConversionExpression.GetUserDTOConversionExpression())
.ToList();
对于UserExDTO;
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?
推荐答案
我已经解决了我的问题,而不仅仅是NeinLinq.这是我的解决方案; 您需要先删除嵌套的声明.
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.
};
}
然后使用NeinLinq的To方法定义翻译;
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());
}
这篇关于Linq.Select()中的嵌套表达式方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!