how to handle null values in this linq-sql query
var query = (from student in dataset.Students
where student.subjectId == SubjectId || student.subjectId ==dataset.Subjects.FindBySubjecttId(SubjectId).PrimarySubjectId
select student)
The above Linq to SQL query fails if PrimarySubjectId is null.
PrimarySubjectId can have null values in the database. If there is no record for the subjectId I want the PrimarySubjectId which can be null. How do i handle the null values for Primary开发者_如何学CSubjectId ?
var query = (from student in dataset.Students
where student.subjectId == SubjectId ||
student.subjectId==dataset.Subjects.FindBySubjecttId(SubjectId).PrimarySubjectId
select student)
Since your PrimarySubjectId allows null so as per your linq statement, the property student.subjectid must be allowed to accept null values by making it Nullable subjectId or int? subjectid.
精彩评论