Use the QueryByAttribute class

You can use the QueryByAttribute class to build queries that test a set of columns (attributes) against a set of values. Use this class with the RetrieveMultiple method or the IOrganizationService.RetrieveMultipleRequest method.

The following table lists the properties that you can set to create a query expression using the QueryByAttribute class.

Property Description
EntityName Specifies which type of table is retrieved. A query expression can only retrieve a collection of one table type. You can also pass this value by using the QueryExpression constructor.
ColumnSet Specifies the set of columns (attributes) to retrieve.
Attributes Specifies the set of attributes selected in the query.
Values Specifies the column values to look for when the query is executed.
Orders Specifies the order in which the rows are returned from the query.
PageInfo Specifies the number of pages and the number of rows per page returned from the query.

The following code example shows how to use the QueryByAttribute class.

//  Create query using querybyattribute      
QueryByAttribute querybyexpression = new QueryByAttribute("account");      
querybyexpression.ColumnSet = new ColumnSet("name", "address1_city", "emailaddress1");  
  
//  Attribute to query      
querybyexpression.Attributes.AddRange("address1_city");  
  
//  Value of queried attribute to return      
querybyexpression.Values.AddRange("Detroit");      
  
//  Query passed to the service proxy      
EntityCollection retrieved = _serviceProxy.RetrieveMultiple(querybyexpression);     
  
//  Iterate through returned collection      
foreach (var c in retrieved.Entities)      
{  
      System.Console.WriteLine("Name: " + c.Attributes["name"]);  
      System.Console.WriteLine("Address: " + c.Attributes["address1_city"]);        
      System.Console.WriteLine("E-mail: " + c.Attributes["emailaddress1"]);      
}  
  

See also

Build Queries with QueryExpression
QueryByAttribute