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

    Question

  • I'm receiving the following error on the below linq query (line 9) and I don't understand how to correct it.

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

    All four of the variables in the equals expression are (int) so I'm not sure why this error would result. Does anyone have any suggestions?

    Code Snippet

    var query =
        from WM in db.WO_Mains
        where WM.Mode == "Active"
        join SC in db.StatusCodes on WM.Status equals SC.Ref
        join CV in db.Cust_Vehicles on WM.LicNum equals CV.LicNum
        join CM in db.Cust_Mains on CV.CustRef equals CM.CustRef
        join MK in db.MM_Makes on CV.Make equals MK.RefNum into CVMK
        from MK in CVMK.DefaultIfEmpty()
        join MD in db.MM_Models // <--- RECEIVING ERROR HERE
            on new { CV.Make, CV.Model } equals new { MD.MakeID, MD.RefNum }
        select new { WM.Promised, CM.CustName1, CV.LicNum, MK.Make, MD.ModelCode, CV.MYear };
    Wednesday, August 01, 2007 4:53 PM

Answers

  • To join multi-valued keys you need to construct an anonymous typ on both sides of the 'equals' that is the same type.  The anonymous type initializer expression infers both type and name of members from the expression you supply. In your case the names of the members are different so the types end up being different so C# cannot figure out the common type between the two.

     

    on new { VC.Make, CV.Model } equals new { MD.MakeID, MD.RefNum }

     

    should be

     

    on new { VC.Make, CV.Model } equals new { Make = MD.MakeID, Model = MD.RefNum }

     

    Using the name = value syntax in the initializer you can specify the name the compiler uses when creating the type.  If all members types & names are the same then the anonymous types are the same type.

     

     

    Thursday, August 02, 2007 5:15 AM

All replies

  • To join multi-valued keys you need to construct an anonymous typ on both sides of the 'equals' that is the same type.  The anonymous type initializer expression infers both type and name of members from the expression you supply. In your case the names of the members are different so the types end up being different so C# cannot figure out the common type between the two.

     

    on new { VC.Make, CV.Model } equals new { MD.MakeID, MD.RefNum }

     

    should be

     

    on new { VC.Make, CV.Model } equals new { Make = MD.MakeID, Model = MD.RefNum }

     

    Using the name = value syntax in the initializer you can specify the name the compiler uses when creating the type.  If all members types & names are the same then the anonymous types are the same type.

     

     

    Thursday, August 02, 2007 5:15 AM
  • Thank you Matt, this is working now. I appreciate your help!
    Thursday, August 02, 2007 5:19 PM
  • Also I found a problecm with the casting throws this error

     

    in this case teams.TEAM_ID is a Decimal and territories.TeamId is int

     

    return (from employees in _ctx.Employees

    join territories in _ctx.Alignments

    on employees.EmployeeId equals territories.EmployeeId

    join teams in _ctx.VTeams

    on new { ID = territories.TeamId } equals new { ID = (int) teams.TEAM_ID }

     

     

    Hopefully its useful

    Friday, November 07, 2008 1:02 AM
  • To join multi-valued keys you need to construct an anonymous typ on both sides of the 'equals' that is the same type.  The anonymous type initializer expression infers both type and name of members from the expression you supply. In your case the names of the members are different so the types end up being different so C# cannot figure out the common type between the two.

     

    on new { VC.Make, CV.Model } equals new { MD.MakeID, MD.RefNum }

     

    should be

     

    on new { VC.Make, CV.Model } equals new { Make = MD.MakeID, Model = MD.RefNum }

     

    Using the name = value syntax in the initializer you can specify the name the compiler uses when creating the type.  If all members types & names are the same then the anonymous types are the same type.

     

     


    Thank you a lot...
    Monday, October 03, 2011 10:04 AM
  • I have error coming 'The type of one of the expressions in the join clause is incorrect.  Type inference failed in the call to 'Join' 

    when using the below code    

    DataTable dt = new DataTable();

            dt.Columns.Add("MACAddress",typeof(string));
            dt.Columns.Add("ModelId",typeof(long));
            dt.Columns.Add("StatusId");
            dt.Rows.Add("ASDWER01", 2, "7");
            dt.Rows.Add("ASDWER040", 2, "7");
            dt.Rows.Add("ASDWER03", 2, "7");


            DataClasses2DataContext db = new DataClasses2DataContext();
            List<Item> liEmp = (from a in db.Items select a).ToList();


            List<DataRow> list = dt.AsEnumerable().ToList();





            var match = from a in liEmp
                        join b in list on new
                        {
                            a.MACAddress,
                            a.ModelId
                        }
                        equals new
                        {
                            MacAddress = b["MACAddress"],
                            ModelId    = b["ModelId"]
                        }
                        select new 
                        {
                            MACAddress = a.MACAddress,
                            ModelId     = a.ModelId
                        };

    Please see what i am doing wrong in above code .

    Wednesday, March 21, 2012 6:05 AM
  • Hi Muhammed,

    It might be a difference in capitalization -- "MacAddress" versus "MACAddress" in the example, above.  The names have to match.

    -Steve

    Friday, March 23, 2012 1:25 PM
  • Thanks sir, that's really helpful.

    savsani s.m.

    • Proposed as answer by sagar savsani Monday, April 30, 2012 8:18 AM
    • Unproposed as answer by sagar savsani Monday, April 30, 2012 8:18 AM
    Monday, April 30, 2012 8:17 AM