我尝试加入两个表,最后收到此错误消息。
无法从查询中推断出类型参数。
和
错误12 join子句中某个表达式的类型不正确。呼叫'加入'
时,类型推断失败
以下是代码:
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。
为什么上面的查询不起作用,错误消息是什么意思?
答案 0 :(得分:3)
尝试使匿名类型的成员更明显相同:
on new { Code = packagingMainCustom.ID_Code,
Version = packagingMainCustom.ID_Version }
equals new { Code = packagingMainSync.ID_Code,
Version = packagingMainSync.ID_CurrentVersion }
这里的主要变化是名称现在匹配,顺序也是如此,大概是类型;这意味着这两个地方实际上现在都是相同的匿名类型(匿名类型基本上由成员名称,顺序,类型和声明程序集的组合定义)。