繁体   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