当管理员在结束视图中按下“conlude”按钮时,我希望程序遍历数据库中的每条记录,并检查是否正好有2条记录(不多不少)将候选设置为true。(Rest会是假的)。我以为我可以通过计数循环来实现这一点,该循环遍历每个记录并检查候选是否设置为true或false,但是我如何在ASP.NET MVC中实现这一点??
谢谢你抽出时间
存储过程
ALTER PROCEDURE [dbo].[conclude]
AS
BEGIN
SET NOCOUNT ON;
delete from dbo.Applications
where Candidate=0
END
行动/方法
public ActionResult Conclude(Concluded concluded, Application application)
{
foreach (NEA.Models.Application app in db.Applications.ToList())
{
if (app.Candidate.Count = 2)
{
using (var context = new ApplicationDbContext())
{
var none = context.Applications.SqlQuery("dbo.removeAllButChosen");
return View(none);
}
}
}
return View("Conclude");
}
以下是msdn示例:
static void GetDepartments(String connectionString, Int32 year) {
String commandText = "dbo.GetDepartmentsOfSpecifiedYear";
// Specify the year of StartDate
SqlParameter parameterYear = new SqlParameter("@Year", SqlDbType.Int);
parameterYear.Value = year;
// When the direction of parameter is set as Output, you can get the value after
// executing the command.
SqlParameter parameterBudget = new SqlParameter("@BudgetSum", SqlDbType.Money);
parameterBudget.Direction = ParameterDirection.Output;
using (SqlDataReader reader = SqlHelper.ExecuteReader(connectionString, commandText,
CommandType.StoredProcedure, parameterYear, parameterBudget)) {
Console.WriteLine("{0,-20}{1,-20}{2,-20}{3,-20}", "Name", "Budget", "StartDate",
"Administrator");
while (reader.Read()) {
Console.WriteLine("{0,-20}{1,-20:C}{2,-20:d}{3,-20}", reader["Name"],
reader["Budget"], reader["StartDate"], reader["Administrator"]);
}
}
Console.WriteLine("{0,-20}{1,-20:C}", "Sum:", parameterBudget.Value);
}