web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

IndexOutOfRangeException when retrieve record from entity

(0) ShareShare
ReportReport
Posted on by

We're using Microsoft.CrmSdk.CoreAssemblies 7.0.0.1 with earlier bind, when retriving record from entity collection, sometimes we're getting: 

xxx.xxx.Crm.Api.PartyController+<>c__DisplayClass3_0.<DetailsAsync>b__0()]
Unhandled exception System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.MergeEntity(Entity entity)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.<CastIterator>d__97`1.MoveNext()
at Microsoft.Xrm.Sdk.Linq.PagedItemCollection`1.MoveNext()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
at xxx.Crm.BusinessLogic.PartyContext.Get(Identifier identifier) in D:\a\1\s\Application\Vxxx.xxx.Crm.BusinessLogic\PartyContext.cs:line 67
at xxx.Crm.Api.PartyController.<>c__DisplayClass3_0.<DetailsAsync>b__0() in D:\a\1\s\Application\xxx.Crm\Api\PartyController.cs:line 76

The query is quite simple, like: _CrmContext.Parties.FirstOrDefault(p => p.xx_name == "KDIE3242"), where _CrmContext is an instance of early bind Crm class we generated using a MS tool from our

Dynamic 365 CRM, it contains all the entities in our CRM. We're using it in a Web API service, getting this error randomly. Any idea why?

Any suggestion is appreciated.

Regards,

Echo

I have the same question (0)
  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    FirstOrDefault will return first entity record if present and null if no entity record present.

    So when it return default which is nothing but null and then you are trying to find p.xx_name which does not exist so you get error.

    To fix this issue you should have below check condition.

    Entity Parties= _CrmContext.Parties.FirstOrDefault;//Change Entity to respective early bound entity class name

    if (Parties!= null)

    {

    then perform your operation;

    }

    docs.microsoft.com/.../linq-query-examples

    Please mark my answer verified if i were helpful

  • Community Member Profile Picture
    on at

    Hi Bipin:

    Thank you for your reply.

    The Parties is a main entity in our CRM, we know we have thousands of records in Parties,  why do we need check if Parties is not null? It should never null, or is there any special cases that an Entity of CRM Context will be null? Can't think of it.

    What I'm thinking is because this query almost performed for every request made to our service for all the endpoints, is this a co-current issue since the requests from different endpoint are hitting same entity and try to query it at same time? for performance reason, we injected CRM Context as SingleInstance when service start.

    Please suggest.

    Regards,

    Echo

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 81 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans