简体   繁体   中英

LINQ outer join gives error

The following LINQ statement (a simple outer join) in C# throws a wild exception:

var query = 
        from cs in db.scan
        join cp in db.patient on cc.pid equals cp.pid into cpGroup
        from cp2 in cpGroup.DefaultIfEmpty()
        select new 
        { 
            Name = ((cp2 == null) ? 
                String.Empty : 
                cp2.plastname + ", " + cp2.pfirstname), 
            DOB = ((cp2 == null) ? 
                DateTime.MinValue : 
                cp2.pdateofbirth) 
        };

Exception:

Auf die Variable "cp2" vom Typ "Core.patient" wird vom Bereich "" verwiesen, sie ist jedoch nicht definiert.

Translated:

The variable "cp2" of type "Core.patient" is refered to by area "" but it is not defined.

What is wrong here? Btw. this error can not be catched by a surrounding try catch block...

There are probably issues with the join. This is the linqish way to accomplish the same thing.

 var query = 
    from cs in db.scan
    from cp in db.patient
    where cs.pid == cp.pid
    select new 
    { 
        Name = cp.plastname + ", " + cp.pfirstname), 
        DOB = cs.pdateofbirth
    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM