2013-06-28 180 views
-1

我想將下面的SQL查詢轉換成Linq,但出現一些錯誤。該聯接即使我使用linqr轉換和使用相同的工具查詢工作正常將SQL查詢轉換爲Linq - 錯誤

SQL查詢

Select 
pj.JobID, 
ja.IsAssigned, 
pj.JobUniqueID, 
u.FirstName, 
pj.IsApproved, 
pj.IsActive, 
pj.IsQuoted, 
pj.IsAssigned, 
pj.ApprovalDate, 
pj.Description, 
s.ServiceName, 
st.ServiceTypeName 
from jobs pj inner join users u on pj.CustomerUserID=u.UserID 
inner join services s on pj.ServiceID = s.ServiceID 
inner join service_type st on st.ServiceTypeID = pj.ServiceTypeID 
left join (select JobID, IsAssigned from job_assigned) ja on pj.JobID = ja.JobID and ja.IsAssigned =1 
left join (select jobid from job_quotes group by jobid having COUNT(1)<3) j on pj.JobID = j.JobID 


Linq : This part i am putting it in the c# code 

from pj in db.Jobs 
join u in db.Users on new { CustomerUserID = pj.CustomerUserID } equals new { CustomerUserID = u.UserID } 
join ja in (
    (from Job_assigned in db.Job_assigned 
    select new { 
     Job_assigned.IsAssigned, 
     Job_assigned.JobID 
    })) 
     on new { pj.JobID, IsAssigned = 1 } 
    equals new { JobID = Convert.ToInt32(ja.JobID), IsAssigned = (Int64?)Convert.ToInt64(ja.IsAssigned) } into ja_join 
from ja in ja_join.DefaultIfEmpty() 
join j in (
    (from Job_quotes in db.Job_quotes 
    group Job_quotes by new { 
     Job_quotes.JobID 
    } into g 
    where  g.Count() < 3 
    select new { 
     JobID = (System.Int32?)g.Key.JobID 
    })) on new { JobID = pj.JobID } equals new { JobID = Convert.ToInt32(j.JobID) } into j_join 
from j in j_join.DefaultIfEmpty() 
select new { 
    JobID = (System.Int32?)pj.JobID, 
    IsAssigned = (System.Boolean?)ja.IsAssigned, 
    pj.JobUniqueID, 
    u.FirstName, 
    pj.IsApproved, 
    pj.IsActive, 
    pj.IsQuoted, 
    Column1 = pj.IsAssigned, 
    pj.ApprovalDate, 
    pj.Description, 
    pj.Services.ServiceName, 
    pj.Service_type.ServiceTypeName 
} 

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

我收到錯誤就行了上面的LINQ聲明

不工作
join ja in (
    (from Job_assigned in db.Job_assigned 
+2

錯誤信息非常重要,你知道嗎?請提供。此外,當您使用annonymus類型時,所有參數都必須命名。 – CSharpie

回答

0

因爲你的匿名類型需要命名參數,eG這是錯誤的:

(from Job_assigned in db.Job_assigned 
select new { 
    Job_assigned.IsAssigned, 
    Job_assigned.JobID 
})) 

這將是正確的:

(from Job_assigned in db.Job_assigned 
select new { 
    JobAssigned = Job_assigned.IsAssigned, 
    JobID = Job_assigned.JobID 
})) 

而且您的變量的命名是可怕的,請把更多的精力在那裏,大大提高了可讀性。

+0

錯誤'AnonymousType#1'不包含'IsAssigned'的定義,並且沒有找到接受'AnonymousType#1'類型的第一個參數的擴展方法'IsAssigned'(您是否缺少using指令或程序集) –

+0

等於new {JobID = Convert.ToInt32(ja.JobID),IsAssigned =(Int64?)Convert.ToInt64(ja.IsAssigned)} into ja_join –