2016-11-20 65 views
0

我有一個視圖模型不能含蓄字符串轉換成模型類型

public class SubjectOverviewViewModel 
    { 
     [DisplayName("Neptunkód")] 
     public Subject NeptunId { get; set; } 
     public SubjectContent TaFoAdatok { get; set; } 
     public SubjectContent TaAdatok { get; set; } 
     public SubjectContent TaOktatok { get; set; } 
     public SubjectContent TaKurzusok { get; set; } 
     public SubjectContent IrodalomLista { get; set; } 
     public virtual SubjectContent SubjectContent { get; set; } 

     public virtual Subject Subject { get; set; } 
    } 

和位指示:

irfwebpage20161013070934_dbEntities2 db = new irfwebpage20161013070934_dbEntities2(); //dbcontext class 
      List<SubjectOverviewViewModel> list = new List<SubjectOverviewViewModel>(); // to hold list of Customer and order details 
      var customerlist = (from x in db.Subject 
           join c in db.SubjectContent on x.NeptunId equals c.NeptunId 
           select new SubjectOverviewViewModel { NeptunId=c.NeptunId, 
            TaAdatok=c.TaAdatok, 
            TaFoAdatok=c.TaFoAdatok, 
            IrodalomLista=c.IrodalomLista, 
            TaKurzusok=c.TaKurzusok }); 

select new SubjectOverviewViewModel部分我得到的錯誤,例如c.TaAdatok是字符串,並將其不能將其隱式轉換爲模型類型。第一個arguemnt NeptunID並不顯示任何錯誤,儘管它也是一個字符串類型,就像其他的一樣。請幫我卡在這裏。我查看了StackOverflow上的其他線程,但那些我已經實現的解決方案並沒有工作。

public partial class SubjectContent 
    { 
     public string NeptunId { get; set; } 
     public string TaFoAdatok { get; set; } 
     public string TaAdatok { get; set; } 
     public string TaOktatok { get; set; } 
     public string TaKurzusok { get; set; } 
     public string IrodalomLista { get; set; } 

     public virtual Subject Subject { get; set; } 
    } 

public partial class Subject 
    { 
     public string NeptunId { get; set; } 
     public string Name { get; set; } 

     public virtual Node Node { get; set; } 
     public virtual SubjectContent SubjectContent { get; set; } 
     public virtual SubjectRating SubjectRating { get; set; } 

     public static implicit operator Subject(string v) 
     { 
      throw new NotImplementedException(); 
     } 
    } 

Subject和SubjectContent是由實體數據建模的數據庫優先方法創建的。

+0

那麼,不知道'SubjectContent'和'Subject'是什麼,我相信他們不是字符串。我也確定'NeptunId = c.NeptunId'會引發異常。也許你應該描述你想要實現的內容,你希望如何從字符串中創建'Subject/SubjectContent'對象。 –

+0

SziaTamás! SubjectOverviewViewModel.TaAdatok屬性的類型是什麼?也許它不是'string',這就是問題的原因。 –

+0

右鍵 - 例如,顯示類「SubjectOverviewViewModel.TaAdatok」。 – adamshakhabov

回答

0

的問題是,在這裏:

var customerlist = (from x in db.Subject 
          join c in db.SubjectContent on x.NeptunId equals c.NeptunId 
          select new SubjectOverviewViewModel { NeptunId=c.NeptunId, 
           TaAdatok=c.TaAdatok, 
           TaFoAdatok=c.TaFoAdatok, 
           IrodalomLista=c.IrodalomLista, 
           TaKurzusok=c.TaKurzusok }); 

c.TaAdatokstringSubjectOverviewViewModel.TaAdatokSubjectContent類型。

我認爲您的視圖模型中的屬性類型是錯誤的。試試這個:

public class SubjectOverviewViewModel 
{ 
    [DisplayName("Neptunkód")] 
    public string NeptunId { get; set; } 
    public string TaFoAdatok { get; set; } 
    public string TaAdatok { get; set; } 
    public string TaOktatok { get; set; } 
    public string TaKurzusok { get; set; } 
    public string IrodalomLista { get; set; } 
} 

您也可以大大簡化查詢,因爲,作爲評論@GertArnold saind,您已經有了一個Subject導航SubjectContent屬性:

var customerlist = from s in db.Subject 
        let sc = s.SubjectContent 
        select new SubjectOverviewViewModel 
        { 
         NeptunId = sc.NeptunId, 
         TaAdatok = sc.TaAdatok, 
         TaFoAdatok = sc.TaFoAdatok, 
         IrodalomLista = sc.IrodalomLista, 
         TaKurzusok = sc.TaKurzusok 
        }; 

作爲最後的一句話,爲什麼即使它包含有關主題的數據,你是否會調用變量customerlist

相關問題