none
Trying to use Aggregate functions with the EntityDataSource and ListView

    Question

  • I am working on my first project with the Entity Framework and am having some difficulty displaying advanced information with the EntityDataSource and a ListView.

    For example, given the two entities:

    Item
    Name
    Price

    Order
    Number
    Items   <---- Navigation Property to Items contained in the Order

    I want to display a list of all orders with a column with the total number of items in the order and a column with the sum of the prices of all items in the order.

    I am using an EntityDataSource configured as follows:

      <asp:EntityDataSource ID="eds" runat="server" ConnectionString="name=NDSEntities"
        DefaultContainerName="NDSEntities" EnableFlattening="False" Include="Items"
        EntitySetName="Orders">
      </asp:EntityDataSource>
    

    Using a ListView, I can list all orders as follows:

    <asp:ListView ID="lvOrders" runat="server" DataSourceID="eds" ItemPlaceholderID="phOrder">
     <LayoutTemplate>
      <table>
        <thead>
         <tr>
          <th>Order Number</th>
          <th>Num. Items</th>
          <th>Order Amt.</th>
         </tr>
        </thead>
        <tbody>
         <asp:PlaceHolder ID="phOrder" runat="server"></asp:PlaceHolder>
        </tbody>
      </table>
     </LayoutTemplate>
     <ItemTemplate>
      <tr>
       <td>
        <%#Eval("Number")%>
       </td>
       <td>
         &nbsp; <!--Placeholder-->
       </td>
       <td>
         &nbsp; <!--Placeholder-->
       </td>
      </tr>
     </ItemTemplate>
    </asp:ListView>
    
    

    I had trouble figuring out how to display a count of items in the order. I tried using Items.Count() function in select statement in the EntityDataSource but that didn't work. Eventually I figured out I could do the following in my ItemTemplate:

    <%# Eval("Items").Count() %>
    

    Now I am stuck trying to get the sum of the items. I am stuck here. I have tried using

    <%# Eval("Items").Sum(Function(i) i.Price)%>
    

    but I get the following error:

    Public member 'Sum' on type 'EntityCollection(Of Item)' not found.

    This confuses me because I know that Sum is a method of EntityCollection(Of <T>).

    My two questions are as follows:

    Am I getting the item count correctly?

    How should I go about getting the sum of the prices of the items?

    Thanks in advance for any help!

    Thursday, December 30, 2010 6:35 PM

Answers

  • Hello Mgolus,

     

    Welcome to MSDN Forum and thank you for posting here.

    Based on your issue, have you tried to operate your datasource in the .cs file? Then bind the result you've got to listview.

    what I mean is as below:

    Double[] values = new Double[] { 2.2, 2.3, 3.3, 4.1, 5.6 };

          var Aggreg = new
            {
              Count = (from v in values
                 select v).Count(),
              Sum = (from v in values
                   select v).Aggregate((cnt, v) => cnt += v),
            };

    Then bind "Count" and "Sum" to your listview. 

    the above code is just an example, hope it can help you.

     

    Best Regards,
     


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 03, 2011 6:08 AM
    Moderator

All replies

  • Hello Mgolus,

     

    Welcome to MSDN Forum and thank you for posting here.

    Based on your issue, have you tried to operate your datasource in the .cs file? Then bind the result you've got to listview.

    what I mean is as below:

    Double[] values = new Double[] { 2.2, 2.3, 3.3, 4.1, 5.6 };

          var Aggreg = new
            {
              Count = (from v in values
                 select v).Count(),
              Sum = (from v in values
                   select v).Aggregate((cnt, v) => cnt += v),
            };

    Then bind "Count" and "Sum" to your listview. 

    the above code is just an example, hope it can help you.

     

    Best Regards,
     


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 03, 2011 6:08 AM
    Moderator
  • Hi Mogolus,

    I am writing to confirm whether your issue has been solved and whether my solution worked.

    If so, I will mark it as answer and close your thread.  And if you feel that doesn't help you, you can unmark it.

    If you have any more concerns, please feel free to let us know.

     

    Best Regards,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, January 06, 2011 1:57 AM
    Moderator