Linq查询-内部联接的ON子句无法比较两个Guid

时间:2018-09-08 06:14:13

标签: linq

如果您要搜索的人是CIA雇员,请选择CIAJobs.EmployerID,否则请选择People.ID

SELECT 
    case when CIAJobs.EmployeeID IS NULL then People.ID
    else CIAJobs.EmployerID
    end

    FROM  [FMO].[People] AS p
    LEFT JOIN [FMO].[CIAJobs] j

        ON (p.ID = j.[EmployeeID]) 
        AND (j.[relationshipType] = '25a8d79d-377e-4108-8c92-0ef9a2e1ab63') 

    where p.ID = '1b66e032-94b2-e811-96e0-f48c508e38a2' // id of person you search for
            OR 
     j.[EmployeeID] =   '1b66e032-94b2-e811-96e0-f48c508e38a2' // id of person you search for

我尝试在Linq中这样做:

    var a = from l in People
            join x in CIAJobs
            on l.Id equals x.EmployeeID && x.RelationshipTypeGuid equals Guid.Parse('25a8d79d-377e-4108-8c92-0ef9a2e1ab63')
            into gcomplex
            from xx in gcomplex.DefaultIfEmpty()
            select (xx.EmployeeID == null) ? l.EmployeeId : x.EmployerID;
    var b = a.ToList();

为什么查询由于以下块而显示错误:&& x.RelationshipTypeGuid equals Guid.Parse('25a8d79d-377e-4108-8c92-0ef9a2e1ab63')     如果删除此部分,则不会显示任何错误。 错误是:运算符&&不能应用于Guid和Guid类型的操作数。

能否请您从逻辑和语法上帮助我纠正Linq查询?谢谢。

1 个答案:

答案 0 :(得分:0)

在这种情况下,多个条件都不需要join。使用这个

var a = from l in People
        join x in CIAJobs
                  .Where(z=>z.RelationshipTypeGuid
                  .Equals(Guid.Parse('25a8d79d-377e-4108-8c92-0ef9a2e1ab63')))
        on l.Id equals x.EmployeeID
        into gcomplex
        from xx in gcomplex.DefaultIfEmpty()
        select (xx.EmployeeID == null) ? l.EmployeeId : x.EmployerID;
    var b = a.ToList();

但是根据您的问题陈述,应该这样做

var a = from l in People
        join x in CIAJobs
        on l.Id equals x.EmployeeID
        into gcomplex
        from xx in gcomplex.DefaultIfEmpty()
        select (xx == null) ? l.EmployeeId : xx.EmployerID;
    var b = a.ToList();