linq join出错

时间:2010-03-19 16:29:42

标签: c# linq entity-framework join

我有这个linq查询:

var segreterie = from s in db.USR_Utenti join h in db.USR_Accounts
                     on new {s.ID, settings.GruppoSegreteria} 
                     equals new {h.USR_UtentiReference,h.ID_Gruppo} select s;

有这个问题:

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

我该怎么做才能解决它?

2 个答案:

答案 0 :(得分:6)

这两种类型应该是相同的 - 这意味着它们需要与您使用匿名类型相同的属性名称。试试这个:

var segreterie = from s in db.USR_Utenti join h in db.USR_Accounts
                 on new {s.ID, Groups = settings.GruppoSegreteria} 
                 equals new {ID = h.USR_UtentiReference, Groups = h.ID_Gruppo}
                 select s;

假设s.IDh.USR_UtentiReference具有相同的类型,而settings.GruppoSegreteriah.ID_Gruppo也是如此。

答案 1 :(得分:0)

在类似的说明中,如果你在一个可以为空的字段上进行连接(例如,int和int?),你可能需要做类似的事情(其中Field2是表1中的int?和表2中的int):

from l in Table1
join l2 in Table2
  on new { l.Field1, Field2 = (l.Field2.Value == null ? -1 : l.Field2.Value) } equals new { l2.Field1, Field2 = l2.Field2 }