开发者

Using Eval with Sum() and Count() with ASP.Net EntityFramework and ListView

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

O开发者_如何学Pythonrder

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>

In the ItemTemplate of the ListView, I can write the order number as follows:

<%# Eval("Number") %>

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

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?


I know that Sum is a method of EntityCollection(Of )

No it isn't.

It's an extension method defined on Enumerable. You might not have included System.Linq.


use this '<%# Eval("Items.Count") %>'


The right way to do it is like this:

<%# ((ICollection<Item>)Eval("Items")).Sum(i => i.Price) %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜