2013-04-18 27 views
1

我很新的實體框架,我試圖在兩個實體上使用連接子句,如下所示。LinQ錯誤LINQ聯合類型推斷未能調用'聯接'錯誤

var alertlist = from elogAlert in yangkeeDBEntity.Yang_Kee_Logistics_Pte_Ltd_ELog_Tablet_Alert 
         where elogAlert.No_ != null 
         join elogAlertDetail in yangkeeDBEntity. Yang_Kee_Logistics_Pte_Ltd_ELog_Tablet_Alert_Details 
         on elogAlert.No_ == elogAlertDetail.AlertID 



         where elogalertdetail.employee_id == driverid 
         select new 
         { 
          elogalertdetail.employee_id, 
          elogalertdetail.alert_id, 
          elogalertdetail.no_, 
          elogalertdetail.status, 
          elogalertdetail.created_by, 
          elogalertdetail.date_created, 



         }; 

喜從上面的代碼我得到兩個錯誤說

'Error 1 The name 'elogAlertDetail' is not in scope on the left side of 'equals'. Consider swapping the expressions on either side of 'equals'.' and 'linq joint type inference failed to call 'join' error ' 

目前這兩個表不具有任何數據。如果有人能夠幫助我解決這種情況,我會很高興

+0

AlertID和No_是否可能是兩種不同的數據類型? – 2013-04-18 05:57:25

+0

數據類型相同:)謝謝 – Kalanamith 2013-04-18 06:03:56

+1

您非常歡迎先生。 – 2013-04-18 06:15:05

回答

4

您在加入Linq時不能使用==。您需要使用equals

注意,它不是method.Equals(..)keyword

from elogAlert in yangkeeDBEntity.Yang_Kee_Logistics_Pte_Ltd_ELog_Tablet_Alert 
join elogAlertDetail in yangkeeDBEntity.Yang_Kee_Logistics_Pte_Ltd_ELog_Tablet_Alert_Details 

on elogAlert.No_ equals elogAlertDetail.AlertID //this line has equals instead of == 

         where elogAlert.No_ != null 
         where elogalertdetail.employee_id == driverid 
         select new 
         { 
          elogalertdetail.employee_id, 
          elogalertdetail.alert_id, 
          elogalertdetail.no_, 
          elogalertdetail.status, 
          elogalertdetail.created_by, 
          elogalertdetail.date_created, 
         }; 

看那documentaion LINQ的加入

+0

等於不能用於int值你的答案在這裏無關,謝謝 – Kalanamith 2013-04-18 05:45:40

+1

@Kalanamith你是什麼意思?你使用'equals'的源,事實上你*有*在與Linq加入時這麼做。請參閱我的答案中的鏈接。我還發表了評論,向您展示我更改代碼的位置 – 2013-04-18 05:49:02

1

你有錯誤涉及到的周圍加入了等號操作參數的順序。

加入的表必須是等號的RHS,並且LHS必須在您加入的行中。

在這種情況下yangkeeDBEntity不是elogAlert

CF MSDN中的例子

from c in categories 
     join p in products on c equals p.Category into ps 
     from p in ps 
     select new { Category = c, p.ProductName }; 

c是你從加入行中,p.category是要加入到

此外您還需要使用equals而不是==如上所述