我正在使用Entity框架和Linq将结果发布到telerik Gridview。这只是为了学习如何使用实体框架和Linq的不同方式。以前我使用带有主视图类的措辞linq查询将结果返回到telerik网格,现在尝试使用匿名类型返回来获取lambda表示法。
当我在'result'变量上设置断点时,我可以看到我有33行。但是当我将它绑定到我的Telerik Grid时,它会显示4页带有空行的gridview。
以下是我的Telerik Gridview在Aspx页面上的样子: -
<telerik:RadGrid ID="gvwData" runat="server" AllowPaging="true" AllowSorting="true" AllowFilteringByColumn="true" OnNeedDataSource="gvwData_NeedDataSource">
<MasterTableView AutoGenerateColumns ="false" TableLayout="Fixed">
<ColumnGroups>
<telerik:GridColumnGroup Name="GeneralInfo" HeaderText="Offender Information" HeaderStyle-HorizontalAlign="Center"/>
<telerik:GridColumnGroup Name="BillingInfo" HeaderText="Billing Information" HeaderStyle-HorizontalAlign="Center" />
</ColumnGroups>
<HeaderStyle Width="200px" />
<Columns>
<telerik:GridNumericColumn DataField = "Offender.YouthNumber" HeaderText="Offender ID" HeaderStyle-Width="100px" FilterControlWidth="50px" ColumnGroupName="GeneralInfo"/>
<telerik:GridBoundColumn SortExpression ="Offender.LastName" DataField="LastName" HeaderText="LastName" ColumnGroupName="GeneralInfo">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Offender.FirstName" HeaderText="FirstName" ColumnGroupName="GeneralInfo"/>
<telerik:GridBoundColumn DataField="Offender.DateOfBirth" HeaderText="DOB" ColumnGroupName="GeneralInfo"/>
<telerik:GridBoundColumn DataField="Violations.PoliceReportDate" HeaderText="Report Date" ColumnGroupName="GeneralInfo"/>
<telerik:GridBoundColumn DataField="Fee.Amount" HeaderText="Payment Owed" ColumnGroupName="BillingInfo"/>
</Columns>
</MasterTableView>
<PagerStyle AlwaysVisible="true" Mode="NumericPages"/>
</telerik:RadGrid>
以下是我的代码隐藏的内容: -
using (var ctx = new Data.Model.TrackingEntities())
{
var result = ctx.Offenders
.Join(ctx.Fees, Offenders => Offenders.OffenderId, Fees => Fees.OffenderId,
(Offenders, Fees) => new { Offenders = Offenders, Fees = Fees })
.Join(ctx.ViolationOffenders, Offenders => Offenders.Offenders.OffenderId, ViolationOffenders => ViolationOffenders.OffenderId,
(Offenders, ViolationOffenders) => new { Offenders = Offenders, ViolationOffenders = ViolationOffenders })
.Join(ctx.Violations, Violations => Violations.ViolationOffenders.ViolationId, ViolationOffenders => ViolationOffenders.ViolationId,
(Violations, ViolationOffenders) => new { Violations = Violations, ViolationOffenders = ViolationOffenders }).ToList();
gvwData.DataSource = result;
}
Q1)任何人都可以告诉我为什么我在变量中有结果,gridview显示4页信息,但没有显示任何内容。但是,如果我消除了最后的2个连接,我会在gridview中显示行。
Q2)有没有更好的方法来编写带有这么多连接的linq语句?我很乐意学习编写这些长连接linq语句的新的/更紧凑的方法。