首页 > 代码库 > crm2011QueryByAttribute的事例

crm2011QueryByAttribute的事例

    using System;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;

    public class QueryByAttributeHelper
    {
        public void Retrieve(IOrganizationService service)
        {
            //构造查询
            QueryByAttribute query = new QueryByAttribute();
            query.EntityName = "new_supplyaccount";
            query.ColumnSet = new ColumnSet("new_name");

            //查询条件
            query.Attributes.AddRange("statecode");
            query.Values.AddRange(0);

            EntityCollection returnCollection = service.RetrieveMultiple(query);
            if (returnCollection != null && returnCollection.Entities.Count > 0)
            {
                foreach(Entity en in returnCollection.Entities)
                {
                    DisplayEntity(en);
                }
            }
        }

        private void DisplayEntity(Entity entity)
        {
            System.Console.WriteLine("start_________________________________");
            var keyArray = entity.Attributes.Keys;
            foreach (string name in keyArray)
            {
                System.Console.WriteLine("attributeName: " + name + ",attributeValue: " + entity.Attributes[name]);
            }
            System.Console.WriteLine("end_________________________________");
        }
    }