2011-09-07 38 views
3

我试图连接两个表的三列,我得到一个错误加盟:问题用简单的由多列在两个表中使用LINQ

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

我不知道发生了什么事情。我检查了st_year,st_month,st_dayyear,monthday的类型,他们都是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}; 

但是如果我做的:

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

那么有没有错误,一切都很好,但我不知道如何加入比其他两列。

回答

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} 
+0

感谢您的帮助这是工作! – Dmitris