Dmitri S.
Dmitri S.

Reputation: 3438

Problem with simple join by multiple columns in two tables using linq

I am trying to join two tables on three columns and I am getting an error:

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

I am not sure what's going on. I checked the types of st_year, st_month, st_day year, month, and day and they are all int, so I shouldn't be getting the error.

The code is:

var q = from obj in objTimeLine                  
                        join ev in eventsTimeLine 
                        on new {obj.st_year, obj.st_month, obj.st_day} equals new {ev.year, ev.month, ev.day}
                        select new {obj, ev};

If I do however :

var q = from obj in objTimeLine                  
                            join ev in eventsTimeLine 
                            on obj.st_year equals ev.year
                            select new {obj, ev};

Then there is no error and everything is fine, but I don't know how to join than 2 other columns.

Upvotes: 3

Views: 2241

Answers (1)

Aducci
Aducci

Reputation: 26684

You need to make sure the anonymous type have the same property names like so:

on new { Year = obj.st_year, Month = obj.st_month, Day = obj.st_day} 
equals new { Year  = ev.year, Month = ev.month, Day = ev.day}

Upvotes: 6

Related Questions