Question

I try to join two tables and ended up with this error message.

The type arguments cannot be inferred from the query.

and

Error 12 The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'.

Here is the Code:

    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);

This linq query is working:

    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);

Therefore the error message could be related to packagingMainSync.ID_CurrentVersion and packagingMainCustom.ID_Version.

Here is the declaration of both properties:

//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");
        }
    }
}

the only difference between both is the DataAnnotations.Key() Annontations.

Why is the query above not working and what does the error message mean?

Était-ce utile?

La solution

Try making the members in the anonymous type more obviously the same:

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

The main change here is that the names now match, as does the order, as presumably do the types; that means that these are actually now the same anonymous type in both places (anonymous types are essentially defined by the combination of member-names, order, type and declaring assembly).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top