简体   繁体   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]

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 type System.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.

相关问题 类型&#39;System.Int16&#39;的对象不能转换为类型&#39;System.Nullable`1 [System.Int32] - Object of type 'System.Int16' cannot be converted to type 'System.Nullable`1[System.Int32] Linq和Equality Operator:类型'System.Int32'的表达式不能用于'System.Object'类型的参数 - Linq and the Equality Operator: Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' 从“System.Int32”到“System.Nullable”1[[System.Int32, mscorlib]] 的无效转换 - Invalid cast from 'System.Int32' to 'System.Nullable`1[[System.Int32, mscorlib]] 类型'System.Int32'的表达式不能用于方法'Boolean Equals(System.Object)'的'System.Object'类型的参数 - Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)' 类型&#39;System.Int32&#39;的表达式不能用于方法&#39;Boolean Equals(System.Object)&#39;的类型&#39;System.Object&#39;的参数 - Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)' 类型'System.Int32'的表达式不能用于返回类型'System.Object' - Expression of type 'System.Int32' cannot be used for return type 'System.Object' “System.Int32”类型的ParameterExpression不能用于“System.String”类型的委托参数 - ParameterExpression of type 'System.Int32' cannot be used for delegate parameter of type 'System.String' LINQ:System.Int32是一个不可为空的值类型 - LINQ: System.Int32 is a non-nullable value type Expression.Convert:'System.Int64'类型的对象无法转换为'System.Int32'类型 - Expression.Convert: Object of type 'System.Int64' cannot be converted to type 'System.Int32' C#MissingMethodException:找不到类型&#39;System.Int32&#39;上的构造方法 - C# MissingMethodException: Constructor on type 'System.Int32' not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM