Quantcast
Channel: C# – Microsoft Dynamics CRM Blog
Viewing all articles
Browse latest Browse all 14

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

$
0
0

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;
        }


Viewing all articles
Browse latest Browse all 14

Trending Articles