使用linq在两个表中使用多列简单连接的问题

Dmi*_*ris 3 c# linq asp.net linq-to-sql c#-3.0

我试图在三列上加入两个表,我收到一个错误:

error CS1941: The type of one of the expressions in the join clause is incorrect.  Type inference failed in the call to 'Join'.
Run Code Online (Sandbox Code Playgroud)

我不确定发生了什么.我检查的类型st_year,st_month,st_day year,month,和day他们都是int,所以我不应该得到的错误.

代码是:

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};
Run Code Online (Sandbox Code Playgroud)

但是,如果我这样做:

var q = from obj in objTimeLine                  
                            join ev in eventsTimeLine 
                            on obj.st_year equals ev.year
                            select new {obj, ev};
Run Code Online (Sandbox Code Playgroud)

然后没有错误,一切都很好,但我不知道如何加入比其他2列.

Adu*_*cci 6

您需要确保匿名类型具有相同的属性名称,如下所示:

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}
Run Code Online (Sandbox Code Playgroud)