开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜