title: Linq to SharePoint throwing null reference exception
body: <p>In our SharePoint 2010 project we are using Linq to SharePoint to get a list of ConfigurationItems. On our testing environments we never had issues fetching data from this list. On our production environment we are now sometimes (we cannot find a pattern right now) getting a null reference exception when looping through the items in the list.</p>
  
  <p>Below is the exception whe are getting thrown from the Linq to SharePoint code:</p>
  
  <pre>
  Object reference not set to an instance of an object. StackTrace: 
  at Microsoft.SharePoint.Linq.FieldRef.GetHashCode()
  at Microsoft.SharePoint.Linq.FieldRef.FieldRefEqualityComparer.GetHashCode(FieldRef obj)
  at System.Linq.Set`1.InternalGetHashCode(TElement value)  at System.Linq.Set`1.Find(TElement value, Boolean add)  at System.Linq.Enumerable.d__7a`1.MoveNext()
  at System.Linq.Buffer`1..ctor(IEnumerable`1 source)  at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)  at Microsoft.SharePoint.Linq.SelectMappingInfo.GetDistinctMappedFields()
  at Microsoft.SharePoint.Linq.Rules.PushDownProcessor.SelectWithInfoOp.PushDownSelect(Context ctx)
  at Microsoft.SharePoint.Linq.Rules.PushDownProcessor.SelectWithInfoOp.Process(Context ctx)
  at Microsoft.SharePoint.Linq.Rules.GuardedRule`4.c__DisplayClass7.b__6(TSourceBase src, TContext ctx)
  at Microsoft.SharePoint.Linq.Rules.RewriteRule`2.Apply(TNode src, TContext ctx)  at Microsoft.SharePoint.Linq.Rules.CacheRule`3.Apply(TSource src, TContext ctx)  
  at Microsoft.SharePoint.Linq.Rules.PushDownProcessor.b__0(Expression e, Context ctx)
  at Microsoft.SharePoint.Linq.Rules.ChildRule`2.Apply(TNode src, TContext ctx)  at Microsoft.SharePoint.Linq.Rules.PushDownProcessor.b__3(Expression e, Context ctx)
  at Microsoft.SharePoint.Linq.Rules.RewriteRule`2.Apply(TNode src, TContext ctx)  at Microsoft.SharePoint.Linq.Rules.CacheRule`3.Apply(TSource src, TContext ctx)  
  at Microsoft.SharePoint.Linq.SPLinqProvider.Rewrite(Expression expression, List`1& assumptions)
  at Microsoft.SharePoint.Linq.SPLinqProvider.RewriteAndCompile[T](Expression expression, List`1& assumptions)
  at Microsoft.SharePoint.Linq.LinqQuery`1.GetEnumerator()
  at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)  
  at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)  
  at Common.Configuration.ConfigurationRepository.GetConfiguration(String siteUrl) InnerException:
  
  Source: Microsoft.SharePoint.Linq TargetSite: Int32 GetHashCode()
  </pre>
  
  <p>And here the code we use in our GetConfiguration method. </p>
  
  <pre><code>using (SpDataContext dataContext = new SpDataContext(siteUrl))
  {
      result = new ConfigurationModel()
      {
          Configurations = (from item in dataContext.GasportConfiguration
                            select new ConfigurationItem()
                            {
                                Key = item.Key,
                                Value = item.Value,
                                Environment = (Environment)Enum.Parse(typeof(Environment), item.Environment.ToString(), true)
                            }).ToList()
      };
  }
  </code></pre>
  
  <p>Anyone having ideas on how to track this down to what's causing this exception?</p>
  
  <p><strong>UPDATE 31-05-2011:</strong></p>
  
  <p>We found a pattern in which way we can reproduce this behaviour on our production environment. And also on our test environment we had this issue too, from which we extracted some Crash Dump files by using AdPlus. </p>
  
  <p>We see this behaviour occuring after the application pool is recycled. The only way to fix this error is to perform a full IISreset. </p>
  
  <p>In the crashdump analysis I found an exception message that states:
  Exception Code:     0xC0000005
  Exception Information:  The thread tries to read from or write to a virtual address for which it does not have the appropriate access.</p>
  
  <p>Hope that someone could give me some more information on this exception?</p>