简体   繁体   中英

Query Expression Distinct column Dynamics CRM 2011

I have created a query expressions which retrieves all order products associated with an order.

Here is my current query expression:

var query = new QueryExpression("salesorderdetail");
query.ColumnSet = new ColumnSet(new string[] { "salesorderdetailid", "productid", "new_event", "new_inventory", "productdescription" });
query.Criteria.AddCondition("salesorderid", ConditionOperator.Equal, combinedEntity.Id); 
query.Distinct = true;
EntityCollection retrieved = context.OrganizationService.RetrieveMultiple(query);

The problem is that I only want to retrieve data with a unique productid.

Is this possible using QueryExpression? Can anyone show me?

Many thanks.

I am not 100% sure what you are asking, however there is a trick I like to use that may help when writing query expressions:

  1. Navigate to the view that displays the records in question
  2. Click the Advanced Find button in the Ribbon Bar
  3. Configure your “find” until it shows the records you are looking for
  4. Click the Download Fetch XML button in the Ribbon Bar
  5. Open the file with a text viewer (or your fav dev tool)

While this is not “code” per se, it usually contains some good hints on how to write it. Also, there is the option of using a Fetch XML query in your code as well.

Use FetchXML to Construct a Query
http://msdn.microsoft.com/en-us/library/gg328117.aspx

If you just need the distinct set of ProductIds contained within the sales order, couldn't just remove the other columns from the ColumnSet? Like so:

query.ColumnSet = new ColumnSet(new string[] { "productid" });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM