簡體   English   中英

使用存儲過程在linq to sql中選擇count

[英]Select count in linq to sql using a stored procedure

我想要特定值的行數

我的.cs頁面代碼是

var rr_sel_cat = db.selcategories(Convert.ToInt32(8)).ToList();   //store procedure                
string count = Convert.ToString(rr_sel_cat.Count());

我的存儲過程:

ALTER PROCEDURE dbo.selcategories
@subid int
AS
select count(*) from editor_j_inf where sub_h_a=@subid
RETURN

尹恩惠表editor_j_inf有一欄sub_h_a與值1,2,3,4,6,8,9

當我過去傳遞sub_h_a不存在的值“ 58”時

var rr_sel_cat = db.selcategories(Convert.ToInt32(58)).ToList();

它返回計數為1,如果我過去傳遞了sub_h_a存在的值“ 8”

var rr_sel_cat = db.selcategories(Convert.ToInt32(8)).ToList();

它也返回計數1

有什么問題?

嘗試:

var rr_sel_cat = db.selcategories(Convert.ToInt32(8)).ToList();   //store procedure                
string count = Convert.ToString(rr_sel_cat.FirstOrDefault());

因為db.selcategories返回一個計數,所以返回List應該帶有一個元素,而女巫已經是該計數。 這就是為什么列表的元素數始終為1的原因。

或者只是刪除ToList:

string count = db.selcategories(Convert.ToInt32(8)).ToString();

您的SP返回計數,它是一個單一值,因此值是1。

ALTER PROCEDURE dbo.selcategories
@subid int
AS
select * from editor_j_inf where sub_h_a=@subid
RETURN

或只是從SP取回價值,這就是您的數量

檢查db.selcategories(Convert.ToInt32(8))的值,您不需要ToList並在此處計數

var rowCount = db.selcategories(Convert.ToInt32(8));

您可以用以下Linq代替:

int sub_id=8;
from s in editor_j_inf where sub_h_a=subid
select s.count();

暫無
暫無

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

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