Riccardo Pezzolati
Riccardo Pezzolati

Reputation: 359

linq to sql join on multiple columns using lambda error

I tried to translate this query by following the example below: linq to sql join on multiple columns using lambda

from tabA in this.context.TabA
join tabB in this.context.TabB on
new
{
   field1 = tabA.Soc,
   field2 = tabA.ID
}
equals new
{
   field1 = TabB.Soc,
   field2 = TabB.Code
}
select tabA;

with this:

var query= this.context.TabA.Join(
    this.context.TabB,
    s => new { s.Soc, s.ID },
    h => new { h.Soc, h.Code },
    (s, h) => s);

But the Visual Studio Intellisense reports this error which I did not understand (Join signals me in red): Visual Studio error

Upvotes: 1

Views: 209

Answers (1)

juharr
juharr

Reputation: 32266

The names of the anonymous types have to match. In the first query you gave them names field1 and field2. The second query will use Soc\ ID on the first and Soc\ Code on the second while the Soc field will match up the ID name will not match the Code name. So the method syntax equivalent of your query syntax would actually be

var query= this.context.TabA.Join(
    this.context.TabB,
    s => new { field1 = s.Soc, field2 = s.ID },
    h => new { field1 = h.Soc, field2 = h.Code },
    (s, h) => s);

But you only really have to name the second property of the anonymous class since it will take the name Soc for the first in both cases.

Upvotes: 1

Related Questions