[英]Expression of type 'System.Nullable`1[System.Int32]' cannot be used for constructor parameter of type 'System.Int32'\r\nParameter name: arguments[0]
I was doing the code below and EFCore throws 我在做下面的代码,EFCore抛出
Expression of type
System.Nullable'1[System.Int32]
cannot be used for constructor parameter of typeSystem.Int32'\\r\\nParameter name: arguments[0]
类型System.Nullable'1[System.Int32]
表达式不能用于类型System.Int32'\\r\\nParameter name: arguments[0]
构造函数参数System.Int32'\\r\\nParameter name: arguments[0]
var data= await _dbContext.Set<Person>().Select(person =>person.Profile != null ?
new ProfileDto(org.Profile.Id , org.Profile.Nickname) : null).ToListAsync();
A Person either have a profile or none, so Profile property on Person is optional. 一个人没有个人资料或没有个人资料,因此“个人”上的个人资料属性是可选的。
Another work around is to create a static method on ProfileDto, for example, 另一种解决方法是在ProfileDto上创建静态方法,例如,
public class ProfileDto
{
public static ProfileDto CreateFromDb(int id, string nickname)
{
// this is a constuctor.
return new ProfileDto(id,nickname);
}
}
//Then do: //然后做:
{
var data= await _dbContext.Set<Person>().Select(person =>person.Profile != null ?
ProfileDto.CreateFromDb(org.Profile.Id , org.Profile.Nickname) : null).ToListAsync();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.