none
Simple SUM against [DataServiceEntity] objects RRS feed

  • Question

  • LINQ newb here -

    I have a fairly simple class that I'm loading with OData rows from an SSRS Report. A basic version might looks like this:

    public class product

    {

     public string prodname {get; set;}

     public double prodcost {get; set;}

    }

    Anyhow, I got a bunch of these suckers loaded up into a variable "query" after executing the report:

     var query = cxt.Execute<TelcoRow>(new Uri("http://localhost/ReportServer_SSRS?/Rev_ARPU3&rs:Command=Render&rs:Format=ATOM&rc:DataFeed=xAx0x1", UriKind.RelativeOrAbsolute));All

    All I need to do is get a sum of prodcost across all rows sitting in "query". Don't need to Group...just want a sum across everything...I'm finding examples with GROUP BY, or examples using a really simply int array, but nothing close to what I need to do...

    Any help?

     

     

    Thursday, July 1, 2010 12:10 AM

Answers

  • Hello Russell,

     

    Since both Sum and Average make aggregation operation on the data source.   It becomes hard to make them into one call.  Could you make the entities into List<> and retrieve them to the client side?   After that, we can directly query the List<> to get the Sum and Average. 

     

    Good day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, July 8, 2010 8:54 AM
    Moderator

All replies

  • Hello Russell,

     

    If I understand it correctly, the return type of cxt.Execute<>() should implement the IEnumerable<T> interface, so it already supports LINQ to Objects queries.  

    ================================================================================

    var sum = cxt.Execute<product>(new uri(“….”, UriKind.RelativeOrAbsolute)).Sum(p => p.prodcost);

    ================================================================================

    Here I used the lambda expression to build the LINQ query.  

     

    Besides, I noticed you call .Execute<TelcoRow>, is the TelcoRow related to the product class? 

     

    Good day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, July 1, 2010 3:09 AM
    Moderator
  • Thank you!. Yes, TecoRow actually IS the class I'm dealing with - I just wanted to use a very basic class for the sake of my example.
    Thursday, July 1, 2010 10:52 AM
  • Hello Russell,

     

    Does my post solve the issue?   If you have any questions, please feel free to let me know.

     

    Have a nice weekend!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, July 2, 2010 1:15 AM
    Moderator
  • Thanks Lingzhi -

    Yes, it does help, although I see my scenario is more difficult than before. I not only need to get the SUM of one field, I need to get the AVG of another, using the same resultset.

    Since I'm going to be in a Silverlight App, I'll also need to go Async. Therefore, using your design pattern, I'd need to do something like this (call BeginExecute TWICE...once for SUM, once for AVG...the second BeginExecute would happen inside the callback for the SUM):

    cxt.BeginExecute<product>(new uri(“….”, UriKind.RelativeOrAbsolute), CallbackSum, cxt);

    private static void CallbackSum(IAsyncResult result)

    {

       var query = result.AsyncState as DataServiceQUery<TelcoRow>;

        sumRev = query.EndExecute(result).Sum(d => d.RevenueAmount);

        cxt.BeginExecute<product>(new uri(“….”, UriKind.RelativeOrAbsolute), CallbackAVG, cxt);

    }

    private static void CallbackAVG(IAsyncResult result)

    {

       var query = result.AsyncState as DataServiceQuery<TelcoRow>;

        avgRev = query.EndExecute(result).Sum(d => d.ARPU);

    }

    ...this seems sort of clumsy to me. Would it just be smarter for me to return ALL rows as a List in a single callback, then enumerate it to get my SUM and Average? Or is there a smarter way to get the SUM and AVERAGE without me needing to walk all the individual result rows (there aren't that many - maybe 150)


    ThankS!

    Tuesday, July 6, 2010 5:49 PM
  • Hello Russell,

     

    Since both Sum and Average make aggregation operation on the data source.   It becomes hard to make them into one call.  Could you make the entities into List<> and retrieve them to the client side?   After that, we can directly query the List<> to get the Sum and Average. 

     

    Good day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, July 8, 2010 8:54 AM
    Moderator
  • I developed the .Net component AdaptiveLINQ (www.adaptivelinq.com).
    This component provide the .QueryByCube() extension method for any IQueryable object.

    Using this method, you can easily query aggregation (SUM, AVG...) using the OData protocol.

    Monday, June 9, 2014 8:10 PM