C# 将查询结果分配给控制器内的对象
我搜索了这个解决方案,但还没有找到我想要的。我在Visual Studio 2017上使用代码优先实体框架MVC,有一个俱乐部表、学生表和链接成员表。每个俱乐部都有一些会员。每个学生都可以成为许多俱乐部的成员。我想做的是计算俱乐部控制器中与每个俱乐部相关联的学生人数。我在Clubs表中有一个字段TotalMembers来保存此值。然后我想在俱乐部视图中显示结果。这是到目前为止我的代码。欢迎任何帮助 我的俱乐部管理员C# 将查询结果分配给控制器内的对象,c#,entity-framework,linq,asp.net-core-mvc,C#,Entity Framework,Linq,Asp.net Core Mvc,我搜索了这个解决方案,但还没有找到我想要的。我在Visual Studio 2017上使用代码优先实体框架MVC,有一个俱乐部表、学生表和链接成员表。每个俱乐部都有一些会员。每个学生都可以成为许多俱乐部的成员。我想做的是计算俱乐部控制器中与每个俱乐部相关联的学生人数。我在Clubs表中有一个字段TotalMembers来保存此值。然后我想在俱乐部视图中显示结果。这是到目前为止我的代码。欢迎任何帮助 我的俱乐部管理员 public async Task<IActionResult> I
public async Task<IActionResult> Index(string searchString)
{
//count how many members each club has
var count = (from m in _context.Members
join c in _context.Clubs
on m.ClubID equals c.Id
select m.StudentID).ToList().Count();
ViewData["CurrentFilter"] = searchString;
var clubs = from c in _context.Clubs
select c;
if (!String.IsNullOrEmpty(searchString))
{
clubs = clubs.Where(c => c.Name.Contains(searchString));
}
return View(await clubs.AsNoTracking().ToListAsync());
}
公共异步任务索引(字符串搜索字符串)
{
//数一数每个俱乐部有多少会员
var count=(来自_context.Members中的m)
加入c俱乐部
关于m.ClubID等于c.Id
选择m.StudentID).ToList().Count();
ViewData[“CurrentFilter”]=searchString;
var clubs=来自c in_context.clubs
选择c;
如果(!String.IsNullOrEmpty(searchString))
{
clubs=clubs.Where(c=>c.Name.Contains(searchString));
}
返回视图(wait clubs.AsNoTracking().toListSync());
}
还有我的俱乐部索引.cshtml
@model IEnumerable<ClubsAndSocieties.Models.Club>
@{
ViewData["Title"] = "Index";
}
<h2 class="text-center pageHeading">Current Clubs and Societies</h2>
<form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
<a asp-action="Create" id="addClass" class="btn btn-info btn-md">
<span class="glyphicon glyphicon-plus"></span> Add
</a>
<input type="text" placeholder=" Find by name:" name="SearchString" value="@ViewData["currentFilter"]" />
<input type="submit" value="Search" class="btn btn-group-sm" />
<a asp-action="Index" class="btn btn-info btn-md">
<span class="glyphicon glyphicon-refresh"></span> Full List
</a>
</p>
</div>
</form>
<div style="overflow-x:auto;">
<table class="responstable">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Chairperson)
</th>
<th>
@Html.DisplayNameFor(model => model.Secretary)
</th>
<th>
@Html.DisplayNameFor(model => model.Treasurer)
</th>
<th>
@Html.DisplayNameFor(model => model.Phone)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.TotalMembers)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Chairperson)
</td>
<td>
@Html.DisplayFor(modelItem => item.Secretary)
</td>
<td>
@Html.DisplayFor(modelItem => item.Treasurer)
</td>
<td>
@Html.DisplayFor(modelItem => item.Phone)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.TotalMembers)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id" class="btn btn-info btn-md edit"><span class="glyphicon glyphicon-edit"></span> Edit</a>
<a asp-action="Details" asp-route-id="@item.Id" class="btn btn-info btn-md details"><span class="glyphicon glyphicon-info-sign"></span> Details</a>
<a asp-action="Delete" asp-route-id="@item.Id" class="btn btn-info btn-md delete"><span class="glyphicon glyphicon-trash"></span> Delete</a>
</td>
</tr>
}
</tbody>
</table>
</div>
@model IEnumerable
@{
ViewData[“标题”]=“索引”;
}
当前的俱乐部和社团
添加
完整列表
@DisplayNameFor(model=>model.Name)
@DisplayNameFor(model=>model.chairman)
@DisplayNameFor(model=>model.Secretary)
@DisplayNameFor(model=>model.treasure)
@DisplayNameFor(model=>model.Phone)
@DisplayNameFor(model=>model.Email)
@DisplayNameFor(model=>model.TotalMembers)
@foreach(模型中的var项目)
{
@DisplayFor(modelItem=>item.Name)
@DisplayFor(modeleItem=>item.chairman)
@DisplayFor(modeleItem=>item.Secretary)
@DisplayFor(modeleItem=>item.Treasurer)
@DisplayFor(modeleItem=>item.Phone)
@DisplayFor(modelItem=>item.Email)
@DisplayFor(modelItem=>item.TotalMembers)
编辑
细节
删除
}
我的目标是获得count变量的结果,该变量将被放置在俱乐部中并返回,这样我就可以在俱乐部视图中将其显示为“TotalMembers”首先,您的查询只对所有成员进行计数。连接实际上是毫无意义的。如果你想计算每个俱乐部的会员人数,你需要一个GROUPBY子句和GROUPBY,比如俱乐部id或名称
发送到视图的模型只是一个
IEnumerable
,因此实际上没有任何地方可以添加计数。您可能需要使用视图模型,在该模型中可以为计数添加属性,或者只需将计数添加到ViewBag
成员。那么,在您看来,您可以在遍历俱乐部列表时从中提取特定俱乐部的计数。这里有什么问题?您是否有任何异常?我希望将count变量的结果放置在俱乐部中并返回,以便在俱乐部视图中显示为“TotalMembers”。没有执行选项,因为我还没有使用“count”变量。在这一行使用count()
时,您不必使用ToList()
代码>。仅使用Count()
。此外,您还需要通过运行for
循环来设置该模型内的计数。您能给我一个如何执行此操作的示例吗。我是linq和MVC的新手