关于LINQ使用存储过程的问题。

_西瓜皮_ 2009-04-29 03:53:36
使用存储过程写了一个报表,但使用LINQ时不知怎么样使用。
...全文
1034 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
goldnex 2011-05-30
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 yangqidong 的回复:]

这个问题之前没遇到过,晚上也测试了一下,发现如果不是直接查询实际存在的表,Linq to sql没法生成模型类,也就没法返回结果集了。我用了一个变通的办法,通过测试,是可行的。
实际上就是欺骗它。做法:
创建一张表,结构和存储过程最后查询的列一致,比如叫Table1,把存储过程内容注释掉,改成简单的select * from Table1
把存储过程拖入设计器,编译。再把存储过程改回来,就……
[/Quote]

悲剧的是,我的存储过程是动态行列转换,查询结果的列数是动态的。。。所以无法用这种欺骗的方法。。。。
皓月明 2010-06-17
  • 打赏
  • 举报
回复
学习了
vwxyzh 2009-07-14
  • 打赏
  • 举报
回复
只能用15楼的欺骗法,生成时用假的存储过程
gongjinjie 2009-07-14
  • 打赏
  • 举报
回复
直接拖过去
ashou706 2009-07-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 yangqidong 的回复:]
和拖放表一样,放进去之后
XxxDataContext dc = new XxxDataContext();
dc.存储过程();
[/Quote]
同意
pt1314917 2009-07-08
  • 打赏
  • 举报
回复
[Quote=引用楼主 athickhead 的回复:]
使用存储过程写了一个报表,但使用LINQ时不知怎么样使用。
[/Quote]

存储过程过于复杂的时候,linq无法识别出其返回类型的。基本上都是int类型了。以前我也试过好多次,最后没办法。把存储过程改到程序中去写。
广州黑码软件 2009-06-19
  • 打赏
  • 举报
回复
select * from
(select ROW_NUMBER() OVER (ORDER BY aa._id) AS [ROW_NUMBER],aa.* from
(select top (@pageIndex * @pageSize) * from #StockList aa
where aa._goodsInfo like @goodsName) bb
where bb.[Row_Number]>((@pageIndex-1) * @pageSize)

这个放在最后试一下
_西瓜皮_ 2009-05-05
  • 打赏
  • 举报
回复
但看了好多的BLOG写到,会自动生成一个结果类,但我一直没试成功,所以才在这里讨论下。
yangqidong 2009-05-01
  • 打赏
  • 举报
回复
这个问题之前没遇到过,晚上也测试了一下,发现如果不是直接查询实际存在的表,Linq to sql没法生成模型类,也就没法返回结果集了。我用了一个变通的办法,通过测试,是可行的。
实际上就是欺骗它。做法:
创建一张表,结构和存储过程最后查询的列一致,比如叫Table1,把存储过程内容注释掉,改成简单的select * from Table1
把存储过程拖入设计器,编译。再把存储过程改回来,就OK啦,Table1也可以删掉了

当然,直接改designer.cs也可以
_西瓜皮_ 2009-04-30
  • 打赏
  • 举报
回复
我的存储过程中是返回的临时表[多表的字段],如果单是一个表的话就不用存储过程了。
_西瓜皮_ 2009-04-30
  • 打赏
  • 举报
回复

Create proc pr_StockInOutStat
@stockKindId nvarchar(50),
@wareId nvarchar(50),
@goodsName nvarchar(50),
@pageSize int,
@pageIndex int,
@pageCount int output
AS

declare @lastCheckTime datetime
declare @lastCheckId uniqueidentifier

select top 1 @lastCheckTime=盘点日期,@lastCheckId=编号 from w仓库盘点记录
order by 盘点日期 desc

if object_id('tempdb..#StockList') is not null
drop table #StockList

create table #StockList
(
_id int identity,
_goodsInfo nvarchar(500),
_lastStockAmount int default 0,
_buy int default 0,
_profit int default 0,
_moveIn int default 0,
_outMoveIn int default 0,
_return int default 0,
_sale int default 0,
_loss int default 0,
_lose int default 0,
_moveOut int default 0,
_outMoveOut int default 0,
_curStockAmount int default 0,
_stockId uniqueidentifier,
_stockKind uniqueidentifier,
_warehouseId uniqueidentifier
)

insert into #StockList(_goodsInfo,_lastStockAmount,_stockId,_stockKind,_warehouseId)
select a.品名 + '(' + a.规格 + ' ' + a.生产厂家 + ' ' + a.批号 + ' ' + convert(varchar(10),a.有效期,121) + ' ' + cast(a.成本单价 as varchar(20)) + ')' ,
a.帐面数量,a.物品库存编号, b.库存类别编号,b.库房编号
from w仓库盘点记录明细 a
inner join w仓库盘点记录 b on a.父编号=b.编号
where 父编号=@lastCheckId


declare @inType nvarchar(20)
declare @outType nvarchar(20)
declare @goodsInfo nvarchar(500)
declare @Amount int
declare @stockId uniqueidentifier
declare @stockKind uniqueidentifier
declare @warehouseId uniqueidentifier

--本期入库
declare curAAA cursor
for
select a.库存类别编号,a.仓库编号,a.入库方式,b.物品编号,
b.品名 + '(' + b.规格 + ' ' + b.生产厂家 + ' ' + b.批号 + ' ' + convert(varchar(10),b.有效期,121) + ' ' + cast(b.成本单价 as varchar(20)) + ')' as 物品信息,
sum(b.数量) as 数量 from w入库记录 a inner join w入库记录明细 b
on a.编号=b.父编号 where a.登记时间>@lastCheckTime
and a.入库标志=1
group by a.库存类别编号,a.仓库编号,a.入库方式,b.物品编号,
b.品名,b.规格,b.生产厂家,b.批号,b.有效期,b.成本单价

open curAAA
fetch next from curAAA into @stockKind,@warehouseId,@inType,@stockId,@goodsInfo,@Amount
while @@Fetch_Status=0
begin
if(@inType='采购')
if exists(select _id from #StockList where _stockId=@stockId and _stockKind=@stockKind and _warehouseId=@warehouseId and _goodsInfo=@goodsInfo)
update #StockList set _buy=_buy+@Amount
where _stockId=@stockId and _stockKind=@stockKind and _warehouseId=@warehouseId and _goodsInfo=@goodsInfo
else
insert into #StockList (_goodsInfo,_lastStockAmount,_buy,_stockId,_stockKind,_warehouseId)
values(@goodsInfo,0,@Amount,@stockId,@stockKind,@warehouseId)

else if(@inType='盘盈')
if exists(select _id from #StockList where _stockId=@stockId and _stockKind=@stockKind and _warehouseId=@warehouseId and _goodsInfo=@goodsInfo)
update #StockList set _profit=_profit+@Amount
where _stockId=@stockId and _stockKind=@stockKind and _warehouseId=@warehouseId and _goodsInfo=@goodsInfo
else
insert into #StockList (_goodsInfo,_lastStockAmount,_profit,_stockId,_stockKind,_warehouseId)
values(@goodsInfo,0,@Amount,@stockId,@stockKind,@warehouseId)

fetch next from curAAA into @stockKind,@warehouseId,@inType,@stockId,@goodsInfo,@Amount
end
close CurAAA
deallocate CurAAA


--本期出库
declare curAAB cursor
for
select a.库存类别编号,a.仓库编号,a.出库方式,b.物品库存编号,
sum(b.数量) as 数量 from w出库记录 a inner join w出库记录明细 b
on a.编号=b.父编号 where a.登记时间>@lastCheckTime
and a.出库标志=1
group by a.库存类别编号,a.仓库编号,a.出库方式,b.物品库存编号

open curAAB
fetch next from curAAB into @stockKind,@warehouseId,@outType,@stockId,@Amount
while @@Fetch_Status=0
begin
if(@outType='销售')
if exists(select _id from #StockList where _stockId=@stockId and _stockKind=@stockKind and _warehouseId=@warehouseId)
update #StockList set _sale=_sale+@Amount
where _stockId=@stockId and _stockKind=@stockKind and _warehouseId=@warehouseId
else
insert into #StockList (_goodsInfo,_lastStockAmount,_sale,_stockId,_stockKind,_warehouseId)
values(@goodsInfo,0,@Amount,@stockId,@stockKind,@warehouseId)

else if(@outType='盘亏')
if exists(select _id from #StockList where _stockId=@stockId and _stockKind=@stockKind and _warehouseId=@warehouseId)
update #StockList set _loss=_loss+@Amount
where _stockId=@stockId and _stockKind=@stockKind and _warehouseId=@warehouseId
else
insert into #StockList (_goodsInfo,_lastStockAmount,_loss,_stockId,_stockKind,_warehouseId)
values(@goodsInfo,0,@Amount,@stockId,@stockKind,@warehouseId)

fetch next from curAAB into @stockKind,@warehouseId,@outType,@stockId,@Amount
end
close curAAB
deallocate curAAB


--得到本期库存
update #StockList set _curStockAmount=
isnull((select 数量 from w仓库库存 where 编号=_stockId),0)


if(len(@stockKindId)>0)
delete from #StockList where _stockKind<>@stockKindid

if(len(@wareId)>0)
delete from #StockList where _warehouseId<>@wareId


if(@pageSize is null or @pageSize<=0)
set @pageSize=20

if(@pageIndex is null or @pageIndex<=0)
set @pageIndex=1

select @pageCount=count(*) from #StockList

if(@pageCount is null)
set @pageCount=0

declare @ii int
set @ii=(@pageCount-1)/@pageSize + 1
if(@pageIndex>@ii)
set @pageIndex=@ii

set @goodsName=@goodsName+'%'


select * from
(select ROW_NUMBER() OVER (ORDER BY aa._id) AS [ROW_NUMBER],aa.* from
(select top (@pageIndex * @pageSize) * from #StockList aa
where aa._goodsInfo like @goodsName) bb
where bb.[Row_Number]>((@pageIndex-1) * @pageSize)


drop table #StockList
yangqidong 2009-04-30
  • 打赏
  • 举报
回复
天。。。
我想看的是你的存储过程
_西瓜皮_ 2009-04-30
  • 打赏
  • 举报
回复
[Function(Name="dbo.pr_StockInOutStat")]
public int pr_StockInOutStat([Parameter(DbType="NVarChar(50)")] string stockKindId, [Parameter(DbType="NVarChar(50)")] string wareId, [Parameter(DbType="NVarChar(50)")] string goodsName)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), stockKindId, wareId, goodsName);
return ((int)(result.ReturnValue));
}

拖放后生成的代码是这样的,返回是int型,
yangqidong 2009-04-30
  • 打赏
  • 举报
回复
我当然明白你的存储过程在sqlserver执行起来是没问题的,
我想知道你是怎么写的导致LINQ不能正确生成方法
_西瓜皮_ 2009-04-30
  • 打赏
  • 举报
回复
在查询分析器中测试是没问题。
_西瓜皮_ 2009-04-30
  • 打赏
  • 举报
回复
存储过程没问题,就是从几个表中获取数据插入到一个临时表中,显示该临时表。
create table #StockList
(
_id int identity,
_goodsInfo nvarchar(500),
_lastStockAmount int default 0,
_buy int default 0,
_profit int default 0,
_moveIn int default 0,
_outMoveIn int default 0,
_return int default 0,
_sale int default 0,
_loss int default 0,
_lose int default 0,
_moveOut int default 0,
_outMoveOut int default 0,
_curStockAmount int default 0,
_stockId uniqueidentifier,
_stockKind uniqueidentifier,
_warehouseId uniqueidentifier
)

yangqidong 2009-04-30
  • 打赏
  • 举报
回复
存储过程贴出来看看吧
_西瓜皮_ 2009-04-30
  • 打赏
  • 举报
回复
[Function(Name="dbo.pr_StockInOutStat")]
public int pr_StockInOutStat([Parameter(DbType="NVarChar(50)")] string stockKindId, [Parameter(DbType="NVarChar(50)")] string wareId, [Parameter(DbType="NVarChar(50)")] string goodsName)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), stockKindId, wareId, goodsName);
return ((int)(result.ReturnValue));
}

拖放后生成的代码是这样的,返回是int型,
var someObjs = dc.存储过程();
foreach(var obj in someObjs )
{
obj.ProductID //这样取数据
}
这样操作获取不到结果。
yangqidong 2009-04-30
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 Athickhead 的回复:]
我的存储过程中是返回的临时表[多表的字段],如果单是一个表的话就不用存储过程了。
[/Quote]
一样的。。。
yangqidong 2009-04-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 Athickhead 的回复:]
但报表要有返回集合呀。
这个返回集合的类型不知道怎么处理。
[/Quote]
var没用过吗?

比如你在存储过程返回的是select * from Products,每个产品都有ProductID

XxxDataContext dc = new XxxDataContext();
var someObjs = dc.存储过程();
foreach(var obj in someObjs )
{
obj.ProductID //这样取数据
}
加载更多回复(3)

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧