如何在这个linq-sql查询中处理空值

时间:2011-05-24 23:00:09

标签: linq-to-sql

var query = (from student in dataset.Students
             where student.subjectId == SubjectId || student.subjectId ==dataset.Subjects.FindBySubjecttId(SubjectId).PrimarySubjectId                 
                                 select student)

如果PrimarySubjectId为null,则上述Linq to SQL查询将失败。

PrimarySubjectId可以在数据库中具有空值。如果没有subjectId的记录,我想要的PrimarySubjectId可以为null。我如何处理空值 PrimarySubjectId?

1 个答案:

答案 0 :(得分:0)

    var query = (from student in dataset.Students

                 where student.subjectId == SubjectId || 

 student.subjectId==dataset.Subjects.FindBySubjecttId(SubjectId).PrimarySubjectId                 
select student)

由于您的PrimarySubjectId允许null,因此根据您的linq语句,必须允许属性student.subjectid通过使其成为Nullable subjectId或int来接受null值? subjectid。