簡體   English   中英

實體框架:搜索具有空列的數據庫條目

[英]Entity Framework: search for database entries with null columns

我有以下型號:

public class FactCache
{
    public int ID { get; set; }
    public DateTime MonthDate { get; set; }
    public string AttributeKey { get; set; }

    public decimal RawValue { get; set; }
    public decimal ManDayValue { get; set; }
    public decimal FTEValue { get; set; }

    public int? InputResourceID { get; set; }
    public int? PhaseID { get; set; }

    public virtual InputResources InputResource { get; set; }
    public virtual DimPhase Phase { get; set; }
}

如上所示, InputResourceIDPhaseID是可空的可選字段。

我想編寫一個查詢來查找給定日期的第一個條目和AttributeKey ,其中PhaseIDInputResourceID都為null。

我試過以下代碼:

FactCache fact = db.FactCache.FirstOrDefault(
  a => a.InputResourceID == null
  && a.PhaseID == null
  && a.MonthDate == monthDate
  && a.AttributeKey == key);

但是,它返回一個null對象。 編寫上述查詢的正確方法是什么?

我已經檢查了數據庫,確實存在具有null PhaseIDInputResourceID條目。

不確定,但也許......

FactCache fact = db.FactCache.FirstOrDefault(
  a => false == a.InputResourceID.HasValue
  && false == a.PhaseID.HasValue
  && a.MonthDate == monthDate
  && a.AttributeKey == key);

只是一個注釋,我很幸運能夠運行EF版本5或更高版本嘗試此處提出的解決方案: 調用失敗並帶有null參數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM