检查可能的空值Linq查询

时间:2011-05-06 15:10:15

标签: linq linq-to-sql

为了简单起见我有2个表:

Table1 has UserId Email
Table2 has UserId Score

我想过滤掉所有分数> 10.但是,Table2不保证其中包含任何用户。 UserId是Table2上的PK和FK。 UserId是表1上的PK。

我的问题是我需要将所有分数都存在,然后检查它们。

1 个答案:

答案 0 :(得分:1)

请参阅Left Outer Join

var q =

    from user in Table1 

    join s in Table2 on user.UserId  equals score.UserId  into lscore

    from score in lscore.DefaultIfEmpty()

    where score == null || lscore.score < 10

    select new { userId = user.userid,  email = user.Email, Score = score == null ? 0 : lscore.score };