2016-12-31 23 views
0

我嘗試在EF核心中使用存儲過程。我的目的是從數據庫表中獲取行數,但我無法發送多個參數。我得到一個錯誤:EF核心存儲過程:在發佈多個參數時發現'沒有找到隱式類型數組的最佳類型'錯誤

No best type found for implicitly-typed array

其實我不知道如何使用Linq語法。在此先感謝

存儲過程:

create proc sp_getExistorNExistCountStd 
    @Date datetime2(7), 
    @ClassId int, 
    @Absent bit, 
    @Count int out 
as 
begin 
    select @Count = COUNT(*) 
    from RollCalls 
    where DateRollCall = @Date 
     and ClassId = @ClassId 
     and [Absent] = @Absent 

    return @Count 
end 

C#

int ExistStdCount = db.RollCalls.FromSql("sp_getExistorNExistCountStd @p0 ,@p1, @p2", 
         // getting error in this section 
         parameters: new[] {DateTime.Now.Date, classIds[i], true }).FirstOrDefault(); 
+1

改爲使用'new object [] {...}'。 –

+0

備註:您應該**不要**爲存儲過程使用'sp_'前綴。微軟已經保留了這個前綴以供自己使用(參見*命名存儲過程*)](http://msdn.microsoft.com/en-us/library/ms190669%28v=sql.105%29.aspx),以及你將來有可能冒着名字衝突的風險。 [這對你的存儲過程性能也是不利的](http://www.sqlperformance.com/2012/10/t-sql-queries/sp_prefix)。最好只是簡單地避免使用'sp_'並將其他內容用作前綴 - 或者根本沒有前綴! –

回答

0

我沒有找到解決方案使用的輸出參數計算行,但我到了另一種解決方案。這種方法給了我什麼我想

Create proc [dbo].[sp_getExistorNExistCountStd] 
    @Date datetime2(7), 
    @ClassId int, 
    @Absent bit  
    as 
    begin 

     select Id from RollCalls 
     where [email protected] 
     and [email protected] 
     and [Absent][email protected] 
    end 

int ExistStdCount = db.RollCalls.FromSql("sp_getExistorNExistCountStd @p0 ,@p1, @p2", 
      DateTime.Now.Date.ToString("yyyyMMdd"), classIds[i], 0).ToList().Count(); 
0

爲什麼使用存儲過程?
EF生成sql確實很聰明。你嘗試過了:db.RollCalls.Where(rc => DateTime.Now.Date.Equals(rc.DateRollCall)/* && other conditions */).Count()

+0

是的,我知道通過thids方法來計數,但我想使用存儲過程(特別是out參數)來查看兩種方法在性能方面的差異。 –

相關問題