繁体   English   中英

类型'System.Nullable`1 [System.Int32]'的表达式不能用于类型'System.Int32'\\ r \\ n的构造函数参数名称:arguments [0]

[英]Expression of type 'System.Nullable`1[System.Int32]' cannot be used for constructor parameter of type 'System.Int32'\r\nParameter name: arguments[0]

我在做下面的代码,EFCore抛出

类型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();

一个人没有个人资料或没有个人资料,因此“个人”上的个人资料属性是可选的。

另一种解决方法是在ProfileDto上创建静态方法,例如,

public class ProfileDto
{
  public static ProfileDto CreateFromDb(int id, string nickname)
  {
    // this is a constuctor.
     return new ProfileDto(id,nickname);
  }
}

//然后做:

{
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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM