c#-如何解决“不能多次枚举查询结果”?
作者:互联网
如果我尝试编写以下代码:错误返回给我:查询结果不能多次枚举
public void StartTransaction()
{
using (var stockMovementCtx = new StockMovementCtxDataContext())
using (var scope = new TransactionScope())
{
var stockMovementItems = from s in stockMovementCtx.spStockMovementForTransaction(TicketID, ItemTypeNo, ItemID, TransactionType,
FromLocation, ToLocation, Qty, PersonelNo, cuser
)
select s;
ReturnCode = stockMovementItems.First().ReturnCode;
// MessageBox.Show(stockMovementItems.First().ToString());
ReturnMsg = stockMovementItems.First().ReturnMessage;
TransactionType = stockMovementItems.First().TransactionType;
TicketID = stockMovementItems.First().TicketID;
alt text http://i45.tinypic.com/avistc.png
细节:
alt text http://i48.tinypic.com/wss2va.png
详细说明:
alt text http://i48.tinypic.com/waonbq.png
解决方法:
简短修正:
var FirstChild = stockMovementItems.First();
然后
ReturnCode = FirstChild.ReturnCode;
//etc...
要么
(from s in stockMovementCtx.spStockMovementForTransaction(TicketID, ItemTypeNo, ItemID, TransactionType, FromLocation, ToLocation, Qty, PersonelNo, cuser) select s).ToList();
但是后者是对Linq的一种滥用.
更长的答案是,无论stockMovementCtx.spStockMovementForTransaction(..)是什么,它都仅支持正向读取.因此,当您调用First()时,您将前进到下一个结果,并且无法返回.
标签:visual-studio-2008,linq-to-sql,linq,c,visual-studio 来源: https://codeday.me/bug/20191106/1999218.html