2

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.

Kjartan
  • 18,591
  • 15
  • 71
  • 96
  • http://stackoverflow.com/a/19135333/580053 ? – Dennis Oct 04 '13 at 10:03
  • Thanks for the link, @Dennis, but I'm not sure exactly how it relates to this situation. See my update - am I overlooking something in your linked post perhaps? – Kjartan Oct 04 '13 at 11:15
  • It will be better, if you'll post a piece of your real code, instead of pseudo-code. If `ColumnA` is `IEnumerable` (or `IQueryable`), then what means `ColumnA.value`? – Dennis Oct 04 '13 at 12:24
  • I felt I had to simplify the code, as the real code was sort of messy and complicated, and I thought someone might recognize the problem based on this. Perhaps this was too simple though.. In any case, I found the problem eventually (different types, as I should have realized earlier). Thanks again for your input though, your link did in fact point me somewhat in the right direction. – Kjartan Oct 04 '13 at 12:30

1 Answers1

3

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
               }
Kjartan
  • 18,591
  • 15
  • 71
  • 96