title: Entity framework - Index (zero based) must be greater than or equal to zero
body: <p>I've been having error for a while now, and while reading all the other posts her on SO, I didn't get much more cleaver.</p>
  
  <p>When making a database call I get the error:</p>
  
  <pre><code>Index (zero based) must be greater than or equal to zero
  </code></pre>
  
  <p>I have the following method which gives me the error:</p>
  
  <pre><code>    public bool IsUserInRole(User user, string roleName)
      {
          using (var repository = new SaxoPublishingEntities())
          {
              repository.Attach(user);
  
              // THIS CALLS GIVE ME THE ERRROR
              var roles = repository.UserRoles.Include("Users").Where(userRole =&gt;
                  userRole.Name == roleName &amp;&amp; userRole.Users.Contains(user)).ToList();
  
  
              if (roles.Any())
              {
                  return true;
              }
              return false;
          }
      }
  </code></pre>
  
  <p>My database model:</p>
  
  <p><img src="https://i.stack.imgur.com/SpCiR.png" alt="enter image description here"></p>
  
  <p>My stack trace:</p>
  
  <pre><code>[FormatException: Indekset (nulbaseret) skal være større end eller lig med nul og mindre end størrelsen af argumentlisten.]
     System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args) +10610374
     System.String.Format(IFormatProvider provider, String format, Object[] args) +63
     System.Data.EntityRes.GetString(String name, Object[] args) +363
     System.Data.Entity.Strings.ELinq_UnsupportedConstant(Object p0) +95
     System.Data.Objects.ELinq.ConstantTranslator.TypedTranslate(ExpressionConverter parent, ConstantExpression linq) +1256
     System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) +88
     System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) +162
     System.Data.Objects.ELinq.ContainsTranslator.TranslateContains(ExpressionConverter parent, Expression sourceExpression, Expression valueExpression) +136
     System.Data.Objects.ELinq.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) +1011
     System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) +88
     System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) +162
     System.Data.Objects.ELinq.BinaryTranslator.TypedTranslate(ExpressionConverter parent, BinaryExpression linq) +122
     System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) +88
     System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) +162
     System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input) +168
     System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input, DbExpressionBinding&amp; binding) +161
     System.Data.Objects.ELinq.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression&amp; source, DbExpressionBinding&amp; sourceBinding, DbExpression&amp; lambda) +168
     System.Data.Objects.ELinq.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call) +66
     System.Data.Objects.ELinq.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod) +47
     System.Data.Objects.ELinq.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) +169
     System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) +88
     System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) +162
     System.Data.Objects.ELinq.ExpressionConverter.TranslateSet(Expression linq) +53
     System.Data.Objects.ELinq.UnarySequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call) +197
     System.Data.Objects.ELinq.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod) +47
     System.Data.Objects.ELinq.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) +169
     System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) +88
     System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) +162
     System.Data.Objects.ELinq.ExpressionConverter.Convert() +50
     System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption) +593
     System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) +255
     System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable&lt;T&gt;.GetEnumerator() +89
     System.Linq.Enumerable.Single(IEnumerable`1 source) +100
     System.Data.Objects.ELinq.ObjectQueryProvider.&lt;GetElementFunction&gt;b__3(IEnumerable`1 sequence) +31
     System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle(IEnumerable`1 query, Expression queryRoot) +56
     System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute(Expression expression) +97
     System.Linq.Queryable.Any(IQueryable`1 source) +233
     Saxo.Publishing.Business.UserServiceOLD.IsUserInRole(User user, String roleName) in c:\Users\laho\Documents\GitHub\SaxoPublishing\Development\Saxo Publishing\Saxo.Publishing.Business\UserServiceOLD.cs:175
     Saxo.Publishing.Web.Security.UserPrincipal.IsInRole(String role) in c:\Users\laho\Documents\GitHub\SaxoPublishing\Development\Saxo Publishing\Saxo.Publishing.Web\Security\User\UserPrincipal.cs:42
     System.Linq.Enumerable.Any(IEnumerable`1 source, Func`2 predicate) +146
     System.Web.Mvc.AuthorizeAttribute.AuthorizeCore(HttpContextBase httpContext) +333
     System.Web.Mvc.AuthorizeAttribute.OnAuthorization(AuthorizationContext filterContext) +421
     System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor) +143
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass25.&lt;BeginInvokeAction&gt;b__1e(AsyncCallback asyncCallback, Object asyncState) +978
     System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +161
     System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +92
     System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag) +53
     System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +538
     System.Web.Mvc.&lt;&gt;c__DisplayClass1d.&lt;BeginExecuteCore&gt;b__17(AsyncCallback asyncCallback, Object asyncState) +83
     System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +161
     System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +92
     System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag, Int32 timeout) +97
     System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +53
     System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +531
     System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +161
     System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +92
     System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag, Int32 timeout) +97
     System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +53
     System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +447
     System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +49
     System.Web.Mvc.&lt;&gt;c__DisplayClass8.&lt;BeginProcessRequest&gt;b__2(AsyncCallback asyncCallback, Object asyncState) +127
     System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +161
     System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +92
     System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag, Int32 timeout) +97
     System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +53
     System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +388
     System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +88
     System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +50
     System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
     System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +155
  </code></pre>
  
  <p>Any ideas is HIGHLY appreciated! :-)</p>
  
  <p><strong>Attempted rewrite version 1:</strong></p>
  
  <pre><code>public bool IsUserInRole(User user, string roleName)
      {
          using (var repository = new SaxoPublishingEntities())
          {
  
              var users = repository.Users.Where(c =&gt; c.Id == user.Id);
  
              if (users.Any())
              {
                  var myUser = users.First();
                  var roles = repository.UserRoles.Where(userRole =&gt;
                      userRole.Name == roleName &amp;&amp; userRole.Users.Contains(myUser)).ToList();
  
                  if (roles.Any())
                  {
                      return true;
                  }
              }
              return false;
          }
      }
  </code></pre>