locked
Query Expression Distinct column Plugin Help. RRS feed

  • Question

  • 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.

    Friday, January 24, 2014 11:54 PM

All replies

  • Hi,
    I'm not sure that I understood completely your question, however if you want you can add a condition on the productid, like

    Guid selectedProductId = new Guid(""); // your guid here
    query.Criteria.AddCondition("productid", ConditionOperator.Equal, selectedProductId);

    and if you don't have the productid, but a product field value, you can always link the product entity, you can find an example here:

    http://srmscrm.wordpress.com/2012/11/05/retrive-entitydata-using-queryexpression-uisng-linkentities-in-ms-crm-2011/


    My blog: www.crmanswers.net - Rockstar 365 Profile

    • Proposed as answer by Guido PreiteMVP Saturday, January 25, 2014 12:51 AM
    Saturday, January 25, 2014 12:51 AM
  • In my opinion it is not possible via queryexpression , instead use fetch query to do so.

    Example:

    <fetch mapping="logical"  distinct="true" version="1.0">
        <entity name="salesorderdetail">
            <attribute name="productid" />
            <filter>
                <condition attribute="salesorderid" operator="eq" value="188e0cc5-bd92-e111-bf0d-000c29b88bc5" />
            </filter>
        </entity>
    </fetch>


    Hope this helps. ----------------------------------------------------------------------- Santosh Bhagat If this post answers your question, please click "Mark As Answer" on the post and "Vote as Helpful"

    Saturday, January 25, 2014 4:03 AM
  • It may help if you elaborate on what you want to achieve, but be aware that Distinct=true in a QueryExpression or Fetch solely ensures that the same record is not returned more than once. Unlike a SQL Distinct clause, it does not prevent the results showing duplicates acorss all fields returned

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Wednesday, January 29, 2014 1:39 PM
    Moderator