久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

奇怪的 LINQ 異常(索引越界)

Strange LINQ Exception (Index out of bounds)(奇怪的 LINQ 異常(索引越界))
本文介紹了奇怪的 LINQ 異常(索引越界)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一張桌子,我們叫Users.這個表有一個在 SQL Server 中定義的主鍵 - 一個自動增量 int ID.

I have a table, we'll call Users. This table has a single primary key defined in SQL Server - an autoincrement int ID.

有時,我對這個表的 LINQ 查詢會失敗,并出現 索引超出范圍" 錯誤 - 即使是最簡單的查詢.查詢本身不使用任何索引器.

Sometimes, my LINQ queries against this table fail with an "Index was outside the range" error - even the most simplest of queries. The query itself doesn't use any indexers.

例如:

User = Users.Take(1);

IEnumerable<Users> = Users.ToList();

兩個查詢都拋出了相同的錯誤.使用調試器 Visualizer 查看生成的查詢 - 我將查詢復制并粘貼到 SQL 中,它工作正常.我還在可視化器上單擊執行",它工作正常.但是單獨執行代碼會引發此錯誤.我沒有在類上實現任何部分方法,所以那里什么也沒有發生.如果我重新啟動調試器,問題就會消失,只是在幾個小時后隨機再次抬起頭.更重要的是,我在生產中運行的應用程序的錯誤日志中看到了這個錯誤.

Both of the queries threw the same error. Using the debugger Visualizer to look at the generated query - I copy and paste the query in SQL and it works fine. I also click "execute" on the visualizer and it works fine. But executing the code by itself throws this error. I don't implement any of the partial methods on the class, so nothing is happening there. If I restart my debugger, the problem goes away, only to rear it's head again randomly a few hours later. More critically, I see this bug in my error logs from the app running in production.

我在我的應用程序中針對數據庫中的十幾個不同實體執行了大量 LINQ,但我只在與表中特定實體相關的查詢中看到此問題.一些谷歌搜索表明這個問題可能與我的模型和另一個實體之間指定的不正確關系有關,但我與這個對象沒有任何關系.它似乎 95% 的時間都在工作,只有另外 5% 的時間失敗了.

I do a ton of LINQ in my app, against a dozen or so different entities in my database, but I only see this problem on queries related to a specific entity in my table. Some googling has suggested that this problem might be related to an incorrect relationship specified between my model and another entity, but I don't have any relationships with this object. It seems to be working 95% of the time, it's just the other 5% that fail.

我已經從設計器中完全刪除了對象,并從刷新"的服務器瀏覽器中重新添加了它,但并沒有解決問題.

I have completely deleted the object from the designer, and re-added it from a "refreshed" server browser, and that did not fix the problem.

知道這里發生了什么嗎?

Any ideas what's going on here?

這是完整的錯誤消息和堆棧跟蹤:

Here's the full error message and stack trace:

索引超出范圍.必須是非負的并且小于集合.參數名稱:index atSystem.Data.Linq.SqlClient.SqlProvider.Execute(表達式查詢,QueryInfo queryInfo, IObjectReaderFactory 工廠, Object[]parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object最后結果)在System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(表達式查詢,QueryInfo[] queryInfos、IObjectReaderFactory 工廠、Object[]userArguments, ICompiledSubQuery[] subQueries) 在System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(表達式查詢)在System.Data.Linq.Table1.System.Linq.IQueryProvider.Execute[TResult](表達式表達式)在System.Linq.Queryable.FirstOrDefault[TSource](IQueryable1 源,表達式`1 謂詞)在 MyProject.FindUserByType(String typeId)

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) at System.Data.Linq.Table1.System.Linq.IQueryProvider.Execute[TResult](Expression expression) at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable1 source, Expression`1 predicate) at MyProject.FindUserByType(String typeId)

根據要求,下面是表架構的副本.

As requested, below is a copy of the table schema.

CREATE TABLE [dbo].[Container](
[ID] [int] IDENTITY(1,1) NOT NULL,
[MarketCode] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Description] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Capacity] [int] NOT NULL,
[Volume] [float] NOT NULL
 CONSTRAINT [PK_Container] PRIMARY KEY CLUSTERED 
(
[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

堆棧跟蹤顯示 FirstOrDefault,但我使用 Take()ToList() 復制了錯誤.所有這些之間的堆棧跟蹤是相同的,只需互換 FirstOrDefault/Take/ToList.將堆棧向下移動到 SqlProvider.Execute 實際上是相同的.

The stack trace shows FirstOrDefault, but I duplicated the error using both Take() and ToList(). The stack trace is identical between all of these, simply interchangnig FirstOrDefault/Take/ToList. The move down the stack to SqlProvider.Execute is in fact identical.

推薦答案

這幾乎肯定不會是每個人的根本原??因,但我在我的項目中遇到了這個完全相同的異常 - 并發現根本原因是一個異常在實體類的構造過程中被拋出.奇怪的是,真正的異常是丟失"的,而是表現為一個 ArgumentOutOfRange 異常,該異常源自檢索對象的 Linq 語句的迭代器.

This almost certainly won't be everyone's root cause, but I encountered this exact same exception in my project - and found that the root cause was that an exception was being thrown during construction of an entity class. Oddly, the true exception is "lost" and instead manifests as an ArgumentOutOfRange exception originating at the iterator of the Linq statement that retrieves the object/s.

如果您收到此錯誤并且在 POCO 上引入了 OnCreated 或 OnLoaded 方法,請嘗試逐步執行這些方法.

If you are receiving this error and you have introduced OnCreated or OnLoaded methods on your POCOs, try stepping through those methods.

這篇關于奇怪的 LINQ 異常(索引越界)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Why shouldn#39;t I always use nullable types in C#(為什么我不應該總是在 C# 中使用可空類型)
C# HasValue vs !=null(C# HasValue vs !=null)
C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有沒有更高效的語法?)
How to set null value to int in c#?(如何在c#中將空值設置為int?)
How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 時如何處理 LINQ 中的空值?)
Method call if not null in C#(在 C# 中如果不為 null 的方法調用)
主站蜘蛛池模板: 成人乱人乱一区二区三区软件 | 色视频www在线播放国产人成 | 精品国产乱码久久久久久牛牛 | 天天玩夜夜操 | 中文字幕一区二区三区精彩视频 | 国产精品一区二区电影 | 日韩在线不卡 | 亚洲精品在线免费 | 人人天天操 | 日韩在线小视频 | 国产精品一区二区欧美黑人喷潮水 | 欧美成人aaa级毛片在线视频 | 日本小电影在线 | 久久激情视频 | 国产精品毛片久久久久久久 | 国产一区二区欧美 | 不卡视频一区二区三区 | 中文字幕国产在线 | 欧美在线一区二区三区 | 亚洲精品视频在线看 | 亚洲人人| 黄色中文字幕 | 久久精品一区二区 | 中文字幕精品一区二区三区在线 | 少妇一级淫片免费播放 | 久久久国产一区二区三区 | 欧美在线一区二区三区 | 999精品视频在线观看 | 亚洲成人免费视频在线观看 | 国产视频第一页 | 久久久精品网站 | 一级毛片在线视频 | 色网在线观看 | 日韩av成人在线观看 | 亚洲男人天堂av | 亚洲欧洲成人在线 | 九九九久久国产免费 | 成人深夜福利网站 | 国产精品高潮呻吟久久aⅴ码 | 天天av网| 国产精品久久在线 |