无法成功将存储过程转换为Linq表达式

时间:2019-05-10 23:09:13

标签: c# sql-server linq

如何将该SQL Server存储过程转换为linq表达式?我有几个错误,但不知道如何解决。

这是存储过程:

    @MatterNumber NVARCHAR(20)
AS
    DECLARE @ClientNumber INT = (SELECT TOP 1 LeadPlaintiffNumber 
                                 FROM [dbo].[vw_cmp_case_numbers] 
                                 WHERE MatterNumber = @MatterNumber)

    SELECT DISTINCT 
        defendantid, 
        defendantcode, 
        defendantname DefendantName 
    FROM   
        (SELECT DISTINCT 
             fmrp.employerid DefendantId, 
             fmrp.employercode DefendantCode, 
             fmrp.employername DefendantName 
         FROM   
             vw_mpid_records fmr 
         LEFT JOIN 
             vw_mpid_records_products fmrp ON fmr.recordid = fmrp.recordid 
         INNER JOIN 
             vw_cmp_event_history fceh ON fmr.jobsitecode = fceh.jobsitecode 
                                       AND fmr.startdate < = fceh.enddate 
         WHERE  
             fceh.clientnumber = @ClientNumber 
             AND fmrp.employerid IS NOT NULL 
             AND fmrp.employercode IS NOT NULL 
         GROUP BY 
             fmrp.employerid, fmrp.employercode, fmrp.employername) yyy 
ORDER BY 
    defendantname 

这是我到目前为止对linq的了解,但是

fmr.StartDate <= fceh.EndDate

然后我也不确定分组依据

var @clientNumber = (from ccn in context.VwCmpCaseNumbers where ccn.MatterNumber == text select ccn).Take(1);

var innerQuery = from fmr in context.VwMpidRecords
                 join fmrp in context.VwMpidRecordsProducts on fmr.Id equals fmrp.Id
                 into gj
                 from x in gj.DefaultIfEmpty()
                 join fceh in context.VwCmpEventHistorys on fmr.JobsiteCode equals fceh.JobsiteCode && fmr.StartDate <= fceh.EndDate
                 where fceh.ClientNumber = @clientNumber &&
                       fmrp.EmployerID != null && 
                       fmrp.EmployerCode != null
                 group fmrp.by fmrp.EmployerID && fmrp.EmployerCode && fmrp.EmployerName

var outerQuery = (from r in innerQuery 
                  select new 
                         {
                             EmployerId = r.EmployerID,
                             EmployerCode = r.EmployerCode,
                             EmployerName = r.EmployerName
                         }).OrderBy(obj => obj.DefendantName);

var viewModel = outerQuery.Select(obj => new SelectOption 
                                             {
                                                   Text = obj.DefendantCode,
                                                   Value = obj.DefendantId,
                                             });

以下是显示错误的行

enter image description here

1 个答案:

答案 0 :(得分:1)

LINQ不允许您使用任何所需的条件“联接”:联接只能具有[this] equals [that]形式。将与该模式不匹配的所有条件移到where子句中。 (这不会影响SQL查询的性能。)

另外,group by个值需要包含在一个(匿名)对象中,而不是&&在一起。

var innerQuery = from fmr in context.VwMpidRecords
join fmrp in context.VwMpidRecordsProducts
on fmr.Id equals fmrp.Id
join fceh in context.VwCmpEventHistorys
on fmr.JobsiteCode equals fceh.JobsiteCode
where fmr.StartDate <= fceh.EndDate
where fceh.ClientNumber = @clientNumber
where fmrp.EmployerID != null && fmrp.EmployerCode != null
group fmrp by new {fmrp.EmployerID, fmrp.EmployerCode, fmrp.EmployerName};