How to add Count of child table in EntityDataSourc

2019-07-27 12:19发布

问题:

I have an EntityDataSource that works to get row data from tblOrderFile as follows:

<asp:EntityDataSource ID="entityDataSourcePreorder" runat="server" 
        ConnectionString="name=iDBEntities" 
        DefaultContainerName="iDBEntities" EnableFlattening="False" 
        EntitySetName="tblOrderFiles" 
        Select="it.[pkOrderFileID], it.[fkOrderFileStatusID], it.[Filename], it.[CreateDate], it.[UserId]" 
        AutoGenerateWhereClause="True" EntityTypeFilter="" Where="">

I would now like to modify it to also return back the number of rows in child table tblOrderFileItem (with Entity Set Name tblOrderFileItems).

I found a way to get the Count to work by adding an Include directive as follows:

    <asp:EntityDataSource ID="entityDataSourcePreorder" runat="server" 
        ConnectionString="name=iDBEntities" 
        DefaultContainerName="iDBEntities" EnableFlattening="False" 
        EntitySetName="tblOrderFiles" Include="tblOrderFileItems"
        AutoGenerateWhereClause="True" EntityTypeFilter="" Where="" >
    </asp:EntityDataSource>

but I believe this is returning the all the columns of all the rows for each Order Item. I only really want the Count and do not want to deliver the rest of the data to the web page.

I also tried simply adding it.tblOrderFileItems.Count to the Select statement but get an error saying

'Count' is not a member of 'Transient.collection[MyDBModel.tblOrderFileItem(Nullable=True,DefaultValue=)]'. To extract a property of a collection element, use a subquery to iterate over the collection.

回答1:

Select="ANYELEMENT(SELECT VALUE Count(c.ItemId) FROM it.tblOrderFileItems AS c) as ChildCount"