【问题标题】:LINQ's WHERE clause where COLUMN may be nullLINQ 的 WHERE 子句,其中 COLUMN 可能为空
【发布时间】:2012-07-18 04:52:42
【问题描述】:

我在使用 LINQ 创建 WHERE 子句时遇到问题。我试图用谷歌搜索它,但如果外部变量是可空整数的类型,我只能得到答案......好吧,我已经尝试过这些方法(我有 0...1 关系在我的数据集中): 例如

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 不支持某些功能的错误...有什么办法可以解决这个问题吗?

【问题讨论】:

    标签: c# mysql winforms linq entity-framework


    【解决方案1】:

    只需使用

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多