Use NoLock and Distinct in Fetch XML and QueryExpression using C# in Dynamics CRM 2011

NoLock: It indicates that no database locks are issued against the data that would prohibit other transactions from modifying the data in the records returned from the query.

Distinct:  It indicates whether results of the query contains duplicate entity instances

Using these in FetchXML and QueryExpression improves performance for checking/fetching results

 


 public static EntityCollection GetAccounts(IOrganizationService service, string accountName)
        {
            QueryExpression queryExp = new QueryExpression();
            queryExp.EntityName = "account";
            var cols = new ColumnSet(new[] { "name", "accountid" });
            queryExp.ColumnSet = cols;

            ConditionExpression conExp = new ConditionExpression();
            conExp.AttributeName = "name";
            conExp.Operator = ConditionOperator.Like;
            conExp.Values.Add(accountName);

            FilterExpression filterExp = new FilterExpression();       
            filterExp.Conditions.Add(conExp);

            queryExp.Criteria = filterExp;
                        
            queryExp.Distinct = false;
            
            queryExp.NoLock = true;

            var accounts = service.RetrieveMultiple(queryExp);

            return accounts;
        }

        public static EntityCollection GetAccounts(IOrganizationService service, string accountName)
        {            
            string fetchXML = string.Format(@"<fetch version='1.0' output-format='xml-platform' mapping='logical' no-lock='true' distinct='false'>
                                                        <entity name='account'>                                                                
                                                            <attribute name='accountid' />
                                                            <attribute name='name' />
                                                            <filter type='and'>
                                                                <condition attribute='name' operator='like' value='{0}' />                                                                                                    
                                                            </filter>
                                                        </entity>
                                                    </fetch>", accountName);

            var fetchExp = new FetchExpression(fetchXML);

            var accounts = service.RetrieveMultiple(fetchExp);

            return accounts;
        }

Advertisements

One thought on “Use NoLock and Distinct in Fetch XML and QueryExpression using C# in Dynamics CRM 2011

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s