2016-01-23 32 views
0

我有一個MVC應用程序,因爲我想根據我的要求從sql server數據庫抓取記錄。主要是我有表「OfficeDetail」,其中一列是「ApprovedDate」,其次是「ChildCount」。 「已批准」包含用戶文檔被批准的日期。第二個「ChildCount」包含整數值。我也在這個網站上搜索了另一個問題,但沒有得到解決方案。我使用兩個條件,所以爲了得到記錄我寫了這段代碼。DbArithmeticExpression參數必須具有數字通用類型。實體框架

public IEnumerable<EmployeeModel> GetAllExpired() 
    { 
     DateTime my = DateTime.Today; 

     DBContext = new ConsumerNomineeFormEntities(); 
     return (from f in DBContext.OfficeDetails 

       where (SqlFunctions.DateDiff("second",f.Date,my.Date)>90) 
       where (f.ChildCount>5) 
       select new EmployeeModel 
        { 
         XConsumer_Id = f.ConsumerNo_, 
         XApproved = f.Date, 
         XChildCount = f.ChildCount, 
         XTimeSpent = (f.Date - DateTime.Today).TotalDays 
        }).ToList(); 
    } 

回答

2

的問題似乎是XTimeSpent的計算,你需要使用sqlfunctions減去日期

+0

日Thnx ..但是,直到我有一個問題根據我的情況,我有一個記錄在表中批准日期是2015-08-20和子女數是3,所以這個日期和今天的日期之間的差異是超過90天。但是我的屏幕上沒有任何記錄,而我有一條記錄 –

+0

好的,但爲什麼不標記此答案已解決?你的問題描述是關於異常,它是用這個答案解決.. – tede24

+0

因爲你的答案不是完美的解決方案...但我給你的學分......那麼我的...... –

0

好吧,我終於得到了,我不得不做出這樣的改變,

DBContext = new ConsumerNomineeFormEntities(); 
     return (from f in DBContext.OfficeDetails 
       where SqlFunctions.DateDiff("day", f.Date, my.Date) > 90 
       where f.ChildCount < 5 
       select new EmployeeModel 
        { 
         XConsumer_Id = f.ConsumerNo_, 
         XApproved = f.Date, 
         XChildCount = f.ChildCount, 
         XTimeSpent = SqlFunctions.DateDiff("day", f.Date, my.Date) 
        }).ToList(); 
相關問題