Joining two tables on more than one column

Posts   
 
    
gudjonv
User
Posts: 27
Joined: 18-Jun-2008
# Posted on: 26-Aug-2008 14:02:47   

Does Linq to LLBLGen Pro support joining two tables on more than one column?

I know that the Linq to SQL syntax is something like this:

var qDef = from a in mData.TableA
                    join b in mData.TableB on new { a.FirstId, a.SecondId } equals new { b.FirstId, b.SecondId }
                    select a;

When I try this with Linq to LLBLGen Pro I get the follwing build error:

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

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 26-Aug-2008 15:32:50   

One of the fields is likely nullable in 1 entity and not nullable in the other. This leads to a difference in types (Nullable<int> vs. int for example) which confuses the C# compiler. You may cast the nullable side to the actual type.

gudjonv
User
Posts: 27
Joined: 18-Jun-2008
# Posted on: 26-Aug-2008 15:42:25   

thanks for the reply,

Both fields are of the type int and they are not nullable (in either entity), I just double checked...

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 26-Aug-2008 16:17:55   

Just to make sure, would you please try the following?

var qDef = from a in mData.TableA
                    join b in mData.TableB 
on new 
{ aFirstId = (int)a.FirstId, 
   asecondId = (int)a.SecondId } 
equals new 
{ bFirstId = (int)b.FirstId, 
   bsecondId = (int)b.SecondId }
                    select a;
gudjonv
User
Posts: 27
Joined: 18-Jun-2008
# Posted on: 26-Aug-2008 16:35:59   

Sure simple_smile

Just tried it and got the same compile error.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39861
Joined: 17-Aug-2003
# Posted on: 26-Aug-2008 17:52:57   

Compile errors are out of reach I'm afraid. I'll see if I can reproduce it. Your query should compile as is.

Frans Bouma | Lead developer LLBLGen Pro
gudjonv
User
Posts: 27
Joined: 18-Jun-2008
# Posted on: 26-Aug-2008 17:57:18   

Thanks,

Here is a little info on the setup I'm using: .NET version = 3.5 Database type = Oracle 10i (10.2.0.1.0) LLBLGen Pro version = 2.6 Final (June 6th, 2008 ) Runtime Libraries (SD.LLBLGen.Pro.ORMSupportClasses.NET20) = 2.6.8.624 Runtime Libraries (SD.LLBLGen.Pro.LinqSupportClasses.NET35) = 2.6.8.814 Runtime Libraries (SD.LLBLGen.Pro.DQE.Oracle10g.NET20) = 2.6.8.72

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39861
Joined: 17-Aug-2003
# Posted on: 26-Aug-2008 18:05:06   

Your query:

var qDef = from a in mData.TableA
                    join b in mData.TableB on new { a.FirstId, a.SecondId } equals new { b.FirstId, b.SecondId }
                    select a;

creates two anonymous types, not 1, I think because the fieldnames aren't exactly the same. Example (which gives the same error: )

var q = from od in metaData.OrderDetail
        join o in metaData.Order on new { od.OrderId, od.ProductId } equals new { o.OrderId, o.EmployeeId.Value }
        select od;

This creates two anonymous types. One with the fields OrderId and ProductId and the other one with OrderId and Value.

As the 'join' keyword is converted by the C# compiler to a .Join() method call, it needs the values in the 'equals' section to be of the same type. As 2 anonymous types are created, this won't work.

This will: var q = from od in metaData.OrderDetail join o in metaData.Order on new { F1 = od.OrderId, F2 = od.ProductId } equals new { F1 = o.OrderId, F2 = o.EmployeeId.Value } select od;

here, just 1 anonymous type is created, with 2 fields, F1 and F2.

Does this solve your problem?

Frans Bouma | Lead developer LLBLGen Pro
gudjonv
User
Posts: 27
Joined: 18-Jun-2008
# Posted on: 26-Aug-2008 18:10:38   

Yes,

This solves the problem.

Thanks again for your quick response.