簡體   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