Kjartan
Kjartan

Reputation: 19101

multiple linq joins with anonymous types using lambda syntax

I've got a query that looks something like this, which works as it should:

ColumnA
    .Join(ColumnB, 
        ColumnA => ColumnA.value, 
        ColumnB => ColumnB.value, 
        (ColumnA, ColumnB) =>  new {ColumnA, ColumnB})
    .Join(ColumnC, 
        join1 => join1.ColumnA.value, 
        ColumnC => ColumnC.value, 
        (join1, ColumnC) =>  new {join1, ColumnC})
    .Join(ColumnD, 
        join2 => join2.ColumnC.Value,
        kobling => ColumnD.Value, 
        (join2, jk1Kobling) => new{  ... })     

Now I need to add an extra column to the second join, and I tried what is shown below. As of yet, I haven't actually added a new column to the join, but I was planning to (/* new col */ in the code). The problem is that Linq is apparently not able to infer the types now. Why is this happening, and is there any simple way to avoid this problem, short of creating a specific type just for the sake of the join?

Error:

The type arguments for method
'System.Linq.Queryable.Join<TOuter,TInner,TKey,TResult>( ...) cannot
be inferred from the usage. Try specifying the type arguments explicitly.

The code I'm trying to run:

ColumnA
    .Join(ColumnB, 
        ColumnA => ColumnA.value, 
        ColumnB => ColumnB.value, 
        (ColumnA, ColumnB) =>  new {ColumnA, ColumnB})
    .Join(ColumnC, 
        join1 => new {join1.ColumnA.value,  /* new col */ }, 
        ColumnC => new {ColumnC.value, /* new col*/ }, 
        (join1, ColumnC) =>  new {join1, ColumnC})
    .Join(ColumnD, 
        join2 => join2.ColumnC.Value,
        kobling => ColumnD.Value, 
        (join2, jk1Kobling) => new{  ... })     

Update in response to the link in the comment below: The problem is related to the two middle lines here:

.Join(ColumnC, 
     join1 => new { join1.ColumnA.value }, 
     ColumnC => new { ColumnC.value }, 
     (join1, ColumnC) =>  new {join1, ColumnC})

I've now tried the following two things, without any changes to the result:

join1 => new { join1.ColumnA.value }, 
         ColumnC => new { value = ColumnC.value }
         join1 => new { value = join1.ColumnA.value }, 
         ColumnC => new { value = ColumnC.value }

I'm still can't figure out how to specify what to compare. Any help would be greatly appreciated.

Upvotes: 2

Views: 3233

Answers (1)

Kjartan
Kjartan

Reputation: 19101

Solved! In case anyone else runs in to similar issues:

The problem in my case was that the datatypes in the columns I was trying to compare were different; one was of type Short, while the other was int (that is, these are the types used in the C# entity classes that represent the tables). In this case, Linq could not decide which type to use when comparing.

The solution: Casting the short value to an int:

join1 => new { value = (int) join1.ColumnA.value }, 
ColumnC => new { value = ColumnC.value }

With the new column added, the solution looks like this:

join1 => new {
               value = (int) join1.ColumnA.value, 
               otherValue =  join1.other
             }, 
ColumnC => new { 
                value = ColumnC.value, 
                otherValue = ColumnC.other
               }

Upvotes: 4

Related Questions