Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange LINQ Exception (Index out of bounds)
    primarykey
    data
    text
    <p>I have a table, we'll call <code>Users</code>. This table has a single primary key defined in SQL Server - an autoincrement <code>int ID</code>.</p> <p>Sometimes, my LINQ queries against this table fail with an <code>"Index was outside the range"</code> error - even the most simplest of queries. The query itself doesn't use any indexers.</p> <p>For example: </p> <pre><code>User = Users.Take(1); </code></pre> <p>or</p> <pre><code>IEnumerable&lt;Users&gt; = Users.ToList(); </code></pre> <p>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. </p> <p>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 <em>any</em> relationships with this object. It seems to be working 95% of the time, it's just the other 5% that fail.</p> <p>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.</p> <p>Any ideas what's going on here?</p> <p>Here's the full error message and stack trace:</p> <blockquote> <p>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.Table<code>1.System.Linq.IQueryProvider.Execute[TResult](Expression expression) at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable</code>1 source, Expression`1 predicate) at MyProject.FindUserByType(String typeId)</p> </blockquote> <p>EDIT: As requested, below is a copy of the table schema.</p> <pre><code>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] </code></pre> <p>EDIT: The stack trace shows <code>FirstOrDefault</code>, but I duplicated the error using both <code>Take()</code> and <code>ToList()</code>. The stack trace is identical between all of these, simply interchangnig <code>FirstOrDefault/Take/ToList</code>. The move down the stack to <code>SqlProvider.Execute</code> is in fact identical. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload