2014-05-15 46 views
2

我嘗試連接兩個表,並以此錯誤消息結束。linq加入時出錯

無法從查詢中推斷出類型參數。

錯誤12聯接子句中的類型的表達式中的一個的不正確。在「加入」的調用中,類型推斷失敗。

這裏是代碼:

var query = (from packagingMainCustom in this.Connection.Tbl_PackagingMaster_Main_Customs 
       join packagingMainSync in this.Connection.Tbl_PackagingMaster_Main_Codes 
        on new { packagingMainCustom.ID_Code, packagingMainCustom.ID_Version } 
        equals new { packagingMainSync.ID_Code, packagingMainSync.ID_CurrentVersion } 
       select packagingMainCustom); 

這LINQ查詢工作:

var query = (from packagingMainCustom in this.Connection.Tbl_PackagingMaster_Main_Customs 
       join packagingMainSync in this.Connection.Tbl_PackagingMaster_Main_Codes 
        on new { packagingMainCustom.ID_Code } 
        equals new { packagingMainSync.ID_Code } 
       select packagingMainCustom); 

因此,錯誤信息可能與packagingMainSync.ID_CurrentVersion和packagingMainCustom.ID_Version。

這裏是兩個屬性的聲明:

//packagingMainCustom 
private long _iD_Version; 
[System.ComponentModel.DataAnnotations.Required()] 
[System.ComponentModel.DataAnnotations.Key()] 
public virtual long ID_Version 
{ 
    get 
    { 
     return this._iD_Version; 
    } 
    set 
    { 
     if(this._iD_Version != value) 
     { 
      this.OnPropertyChanging("ID_Version"); 
      this._iD_Version = value; 
      this.OnPropertyChanged("ID_Version"); 
     } 
    } 
} 

//packagingMainSync 
private long _iD_CurrentVersion; 
[System.ComponentModel.DataAnnotations.Required()] 
public virtual long ID_CurrentVersion 
{ 
    get 
    { 
     return this._iD_CurrentVersion; 
    } 
    set 
    { 
     if(this._iD_CurrentVersion != value) 
     { 
      this.OnPropertyChanging("ID_CurrentVersion"); 
      this._iD_CurrentVersion = value; 
      this.OnPropertyChanged("ID_CurrentVersion"); 
     } 
    } 
} 

兩者之間的唯一區別是DataAnnotations.Key() Annontations。

爲什麼上面的查詢不起作用,錯誤信息是什麼意思?

回答

3

嘗試使在匿名類型的成員更明顯的是相同的:

on new { Code = packagingMainCustom.ID_Code, 
    Version = packagingMainCustom.ID_Version } 
equals new { Code = packagingMainSync.ID_Code, 
    Version = packagingMainSync.ID_CurrentVersion } 

這裏的主要變化是,現在的名稱匹配,一樣的順序,大概做的類型;這意味着這些實際上現在在兩個地方都是相同的匿名類型(匿名類型基本上由成員名稱,順序,類型和聲明程序集的組合來定義)。

+0

它是有道理的。你提供了一個很好的解釋 –