LINQ的WHERE子句,其中COLUMN可以为null

时间:2012-07-18 04:52:42

标签: c# mysql winforms linq entity-framework

我在使用LINQ制作WHERE子句时遇到问题。我试图谷歌它但我只得到如果外部变量是可以为空的整数类型该怎么做的答案...好吧,我已经尝试过这些方法(我有0 ... 1关系在我的数据集中): e.g。

int oldId = oldQuestion.id;
IEnumerable<possible_answer> possibleAnswersQuery =
            from x in Mentor11Entities.possible_answers
            where object.Equals(x.question_id, oldId)
            select x;
List<possible_answer> possibleAnswers = 
            possibleAnswersQuery.ToList<possible_answer>();

int oldId = oldQuestion.id;
IEnumerable<possible_answer> possibleAnswersQuery =
            from x in Mentor11Entities.possible_answers
            where Convert.ToInt32(x.question_id ?? 0).Equals(oldId)
            select x;
List<possible_answer> possibleAnswers = 
            possibleAnswersQuery.ToList<possible_answer>();

但我总是得到LINQ不支持某些功能的错误......有什么方法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:4)

只需使用

where x.question_id != null && x.question_id == oldId

所以你的查询应该是:

IEnumerable<possible_answer> possibleAnswersQuery =
            from x in Mentor11Entities.possible_answers
            where x.question_id != null && x.question_id == oldId
            select x;