选择top(n)2相关表实体框架

时间:2013-02-18 10:00:20

标签: c# asp.net entity-framework

这是我的实体数据源

<asp:EntityDataSource ID="EntityDataSourceCLient" runat="server"
        ConnectionString="name=MBSDbEntities" DefaultContainerName="MBSDbEntities"
        EnableFlattening="False" EntitySetName="ReservationLists" Include="UserProfile" OrderBy="it.Date" Where="">
    </asp:EntityDataSource>

这是我的gridview

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" 
        CellPadding="3" DataKeyNames="ID" DataSourceID="EntityDataSourceCLient">
        <Columns>
            <asp:TemplateField HeaderText="UserID" SortExpression="UserProfile.FirstMidname">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("UserProfile.FirstMidname") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("UserProfile.FirstMidname") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="TransactionType" HeaderText="TransactionType" 
                SortExpression="TransactionType" />
            <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
            <asp:BoundField DataField="Status" HeaderText="Status" 
                SortExpression="Status" />
        </Columns>
        <FooterStyle BackColor="White" ForeColor="#000066" />
        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
        <RowStyle ForeColor="#000066" />
        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#007DBB" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#00547E" />
    </asp:GridView>

我可以显示2个相关表格,但我无法显示具体的数字。你能帮帮我吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

您需要加入该表,然后按字段(可能是TestDate)对其进行排序,然后使用Enumerable.Take()获取最高5条记录。像

这样的东西
var query = (from q in table1 
            join r  in table2 on q.UserID equals r.UserID)
            .OrderBy(s=> s.TestDate)
            .Take(5);