为大学辅导员建立一个调度系统。我编写了一个LINQ查询,该查询在单击按钮时运行,我想返回gridview中的可用插槽。因为查询返回匿名类型,所以数据绑定器会在标题中抛出错误。我已经在这里找到了一个修复程序但是查询在控件中没有任何内容(这里的按钮)所以我不确定如何进行更改。
表:
CREATE TABLE [dbo].[Sessions] (
[sessionID] INT IDENTITY (1, 1) NOT NULL,
[create_date] DATETIME NOT NULL,
[last_update_date] DATETIME NOT NULL,
[tutorCWID] INT NOT NULL,
[semesterID] INT NOT NULL,
[session_date] DATE NOT NULL,
[session_start_time] TIME (7) NOT NULL,
[session_end_time] NCHAR (10) NOT NULL,
[session_bookings] INT NOT NULL,
[session_group_size] INT NOT NULL,
[session_student_report] VARCHAR (500) NULL,
[session_tutor_report] VARCHAR (500) NULL,
[session_documents] VARCHAR (100) NULL,
CONSTRAINT [PK_Session] PRIMARY KEY CLUSTERED ([sessionID] ASC),
CONSTRAINT [FK_Session_UserProfileTutor] FOREIGN KEY ([tutorCWID]) REFERENCES [dbo].[UserProfiles] ([userCWID]),
CONSTRAINT [FK_Session_Semester] FOREIGN KEY ([semesterID]) REFERENCES [dbo].[Semesters] ([semesterID]));
CREATE TABLE [dbo].[UserProfiles] (
[userCWID] INT NOT NULL,
[first_name] VARCHAR (50) NOT NULL,
[last_name] VARCHAR (50) NOT NULL,
[email] VARCHAR (150) NOT NULL,
[phone_mobile] NUMERIC (18) NULL,
[phone_work] NUMERIC (18) NULL,
[status] NCHAR (10) NOT NULL,
[password] VARCHAR (50) NOT NULL,
[user_typeID] INT NOT NULL,
[ImareUrl] VARCHAR (50) NULL,
CONSTRAINT [PK_Tutor] PRIMARY KEY CLUSTERED ([userCWID] ASC),
CONSTRAINT [FK_UserProfile_UserType] FOREIGN KEY ([user_typeID]) REFERENCES [dbo].[UserTypes] ([user_typeID]));
CREATE TABLE [dbo].[UserProfiles_Courses] (
[tutorCWID] INT NOT NULL,
[courseID] VARCHAR (50) NOT NULL,
[note] NVARCHAR (100) NULL,
CONSTRAINT [PK_UserProfiles_Courses] PRIMARY KEY CLUSTERED ([tutorCWID] ASC, [courseID] ASC),
CONSTRAINT [FK_UserProfiles_Courses_Courses] FOREIGN KEY ([courseID]) REFERENCES [dbo].[Courses] ([courseID]),
CONSTRAINT [FK_UserProfiles_Courses_UserProfiles] FOREIGN KEY ([tutorCWID]) REFERENCES [dbo].[UserProfiles] ([userCWID]));
ASP.NET代码背后:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Students_StudentSelectSession : System.Web.UI.Page
{
string _StdSessCourse = "";
DateTime _StdSessDate = DateTime.Now;
string _semester = "";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnStdSessSearch_Click(object sender, EventArgs e)
{
_StdSessCourse = ddlStdSessCourse.SelectedValue;
_StdSessDate = calStdSessDate.SelectedDate;
using (ISDS411_FinalProjectDB_5Entities stdSessEntity = new ISDS411_FinalProjectDB_5Entities())
{
var stdSessAvailable = (from s in stdSessEntity.Sessions
join up in stdSessEntity.UserProfiles on s.tutorCWID equals up.userCWID
join uc in stdSessEntity.UserProfiles_Courses on up.userCWID equals uc.tutorCWID
where uc.courseID == _StdSessCourse && s.session_date == _StdSessDate
orderby s.session_date
select new
{
up.first_name,
up.last_name,
s.session_start_time,
s.session_end_time,
s.session_group_size,
s.session_bookings,
}).FirstOrDefault();
gvStdSessAvailable.DataSource = stdSessAvailable;
gvStdSessAvailable.DataBind();
}
}
}
这是gridview:
<%@ Page Title="Student - Select Tutoring Session" Language="C#" MasterPageFile="~/Students/StudentMasterPage.master" AutoEventWireup="true" CodeFile="StudentSelectSession.aspx.cs" Inherits="Students_StudentSelectSession" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cpMainContent" Runat="Server">
<h1>Select a session</h1>
<p>Use the controls below to select a tutoring session:
<ol>
<li>Select the course you want to be tutored in from the dropdown menu.</li>
<li>Select the date that you are looking for a tutoring session on.</li>
<li>Click the "Search" button.</li>
</ol>
</p>
<table>
<tr>
<td>
<asp:DropDownList ID="ddlStdSessCourse" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource2" DataTextField="courseID" DataValueField="courseID"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ISDS411_FinalProjectDB_5ConnectionString1 %>" SelectCommand="SELECT [courseID] FROM [Courses]"></asp:SqlDataSource>
</td>
</tr>
<tr>
<td>
<asp:Calendar ID="calStdSessDate" runat="server"></asp:Calendar>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnStdSessSearch" runat="server" Text="Search" OnClick="btnStdSessSearch_Click" />
</td>
</tr>
<tr>
<td>
<asp:GridView ID="gvStdSessAvailable" runat="server" DataKeyNames="sessionID" EmptyDataText="There are no data records to display." AllowPaging="True" AllowSorting="True"></asp:GridView>
</td>
</tr>
</table>
<p><a href="~/Students/StudentHome.aspx" runat="server">Go back to Student Homepage</a></p>
我想这需要除了匿名类型之外的东西,但不知道如何将它组合在一起。或者,我无法使用网格视图,但我想利用“选择”功能让学生选择他们想要的辅导课程。
答案 0 :(得分:0)
尝试更改:.FirstOrDefault();
至.AsEnumerable();
&#34;我想这需要的不是匿名类型,但不确定如何将它放在一起#34; 只需创建一个包含属性的新类,并使用它:
var stdSessAvailable = (from s in stdSessEntity.Sessions
join up in stdSessEntity.UserProfiles on s.tutorCWID equals up.userCWID
join uc in stdSessEntity.UserProfiles_Courses on up.userCWID equals uc.tutorCWID
where uc.courseID == _StdSessCourse && s.session_date == _StdSessDate
orderby s.session_date
select new SessAvailable
{
FirstName = up.first_name,
LastName = up.last_name,
SessionStarTime = s.session_start_time,
SessionEndTime = s.session_end_time,
SessionGroupSize = s.session_group_size,
SessionBookings = s.session_bookings,
}).AsEnumerable();