-1

我正在尝试将以下 SQL 查询转换为 Linq,但出现了一些错误。即使我使用 linqr 进行转换并且在同一个工具中查询工作正常,连接也不起作用

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
4

1 回答 1

0

这是因为您的匿名类型需要命名参数,例如这是错误的:

(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
}))

此外,您对变量的命名很糟糕,请在其中付出更多努力以大大提高可读性。

于 2013-06-28T14:55:42.060 回答