2
votes

I want to join two tables. The joining columns are column3, column4 for both the tables.

Visual Studio is throwing the following error:

The type arguments for method

'System.Linq.Enumerable.Join (System.Collections.Generic.IEnumerable, System.Collections.Generic.IEnumerable, System.Func, System.Func, System.Func)'

cannot be inferred from the usage. Try specifying the type arguments explicitly.

My code is as follows:

int i = context.Table1.Where(u => u.column1 == true).Join(
    context.Table2.Where(u => u.column2.ToUpper() == "COMPLETED"),
    q => new { Column3 = q.column3, Column4 = q.column4 },
    u => new { Column3 = u.column3, Column4 = u.column4 },
    (q, u) => new { q.column1 }).Count();

What is wrong with this query?

Thanks in advance.

2

2 Answers

2
votes

The query itself seems fine, but we don't know the full type of context.TableX.

For the Join to compile, both anonymous types defined by the new { Column3 = q.column3, Column4 = q.column4 } parts need to have the same property types, so that the generated anonymous types are the same. Do the entries of Table1 and Table2 have the same type, or may it be that their columns types don't match?

On my machine, the line

l1.Join(l2, 
        d => new { P = d.Column1, Q = d.Column2 }, 
        d => new { P = d.Column1, Q = d.Column2 }, 
        (d, u) => new { d.Column1 }).Count();

compiles fine, with l1 and l2 both being of type List<Dummy> and Dummy just having two integer properties.

2
votes

I've come across this some times, and the solution is simply to cast each column to the correct type:

int i = context.Table1.Where(u => u.column1 == true).Join(
context.Table2.Where(u => u.column2.ToUpper() == "COMPLETED"),
q => new { Column3 = (string)q.column3, Column4 = (decimal?)q.column4 },
u => new { Column3 = (string)u.column3, Column4 = (decimal?)u.column4 },
(q, u) => new { q.column1 }).Count();