locked
Selecting Top1 Record RRS feed

  • Question

  • Hi ,

     

    I have a Query Like

     

    select  dealid from opportunity where dealid != null order by CreatedOn desc

     

    for the above query i am able to get the values as below Method  . But My problem is i need to write the Query as

     

    select top 1 dealid from opportunity where dealid != null order by CreatedOn desc

     

     CAN ANY ONE SUGGEST ME HOW TO IMPLEMENT TOP 1 RECORD IIN QUERY EXPRESSION ,

     

    URGENT PLZ

     

     

     

    protected void Page_Load(object sender, EventArgs e)
    {

    CrmService crm = new CrmService();
    crm.Credentials = new System.Net.NetworkCredential("uid", "pwd", "domainname");

    ColumnSet coloum = new ColumnSet();
    coloum.Attributes = new string[] { "new_dealid", "createdon" };


    //ConditionExpression conditionPrincipal = new ConditionExpression();
    //conditionPrincipal.AttributeName = "new_dealid";
    //conditionPrincipal.Operator = ConditionOperator.OnOrBefore;

    ConditionExpression conditionPrincipal = new ConditionExpression();
    conditionPrincipal.AttributeName = "new_dealid";
    conditionPrincipal.Operator = ConditionOperator.NotNull;

    OrderExpression order = new OrderExpression();
    order.AttributeName = "createdon";
    order.OrderType = OrderType.Descending;

    FilterExpression filterPrincipal = new FilterExpression();
    filterPrincipal.Conditions = new ConditionExpression[] { conditionPrincipal };

    //query.PageInfo = new PagingInfo();
    //query.PageInfo.Count = 1;
    //query.PageInfo.PageNumber = 2;


    QueryExpression query = new QueryExpression();
    query.EntityName = EntityName.opportunity.ToString();
    query.ColumnSet = coloum;
    query.Criteria = filterPrincipal;
    query.Orders = new OrderExpression[] {order};

    //query.PageInfo = new PagingInfo();
    //query.PageInfo.Count = 1;

    BusinessEntityCollection business = crm.RetrieveMultiple(query);
    int count = business.BusinessEntities.Length;


    string strdealid = "";
    foreach (opportunity oppy in business.BusinessEntities)
    {
    strdealid = oppy.new_dealid.ToString();
    Response.Write(strdealid);
    //CrmDateTime cdate = new CrmDateTime();
    //cdate.Value = oppy.createdon.Value;
    break;

    }


    }

     

    Wednesday, July 9, 2008 7:52 AM

Answers

  • It looks like your code is on the right lines - you need to use the PagingInfo class with both the Count and PageNumber set to 1, along with an OrderExpression. I don't see why you've commented out some of your code

    Wednesday, July 9, 2008 8:06 AM
    Moderator