详细信息使用自定义模型通过LINQ查看相关数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了详细信息使用自定义模型通过LINQ查看相关数据相关的知识,希望对你有一定的参考价值。
由于我是ASP的新手,确实很困惑如何通过LINQ获得模型/表的某些特定列的列表,反映在相关父项的详细信息页面上。考虑两个类的示例,例如:
public class Tournament
{
[Key]
public string TournamentID { get; set; }
public DateTime TournamentDate { get; set; }
public string Place { get; set; }
[ForeignKey("TeamA")]
public string TeamAID { get; set; }
public Team TeamA { get; set; }
[ForeignKey("TeamB")]
public string TeamBID { get; set; }
public Team TeamB { get; set; }
}
public class Team
{
[Key]
public string TeamID { get; set; }
public string TeamName { get; set; }
public string Captain { get; set; }
[InverseProperty("TeamA")]
public ICollection<Tournament> TeamA { get; set; }
[InverseProperty("TeamB")]
public ICollection<Tournament> TeamB { get; set; }
}
这两个表格包含了团队的详细信息,以及他们之间的锦标赛。由于锦标赛的模型/表格不止一个领域与团队相关联,因此正在使用InverseProperty和相关的ForeignKeys。
主要目标是在视图中呈现顶部团队的一部分细节,但下面列出的锦标赛的相关条目相同。
由于团队(例如Team_1)可能存在于锦标赛的TeamA列中,甚至存在于TeamB列中,因此会弹出如何以下列方式呈现相同内容的问题:
TeamID:ID_1
TeamName:Team_1
TeamCaptain:队长1
ID |日期|竞争对手|地点
.... | ........ | ................ | .........
.... | ........ | ................ | .........
出于同样的原因,我推断出我应该为Custom Columns使用一些特殊的Model Class,因此添加了以下类:
public class GamesList
{
public string TID { get; set; }
public DateTime TDate { get; set; }
public string TPlace { get; set; }
public string TeamLinks { get; set; }
public string TeamNames { get; set; }
}
和控制器动作如:
public async Task<IActionResult> Index2(string teamId)
{
var gamesList = ((from x in _context.Tournaments
.Where(x => x.TeamAID == teamId)
select new GamesList
{
TID = x.TournamentID,
TDate = x.TournamentDate,
TeamLinks = x.TeamAID,
TeamNames = x.TeamB.TeamName,
TPlace = x.Place
})
.Concat(from x in _context.Tournaments
.Where(x => x.TeamBID == teamId)
select new GamesList
{
TID = x.TournamentID,
TDate = x.TournamentDate,
TeamLinks = x.TeamBID,
TeamNames = x.TeamA.TeamName,
TPlace = x.Place
})).OrderBy(x => x.TDate);
return View(await gamesList.ToListAsync());
}
因此,要将相同的列表连接起来,但是团队ID和名称被翻转,导致所有ID都在名为TeamLinks的属性中编译,而竞争对手的名称在TeamNames中排列。
现在,所述列表可以通过拥有@model IEnumerable来呈现,但是作为主要目标指定了如何在团队的详细信息下呈现这样的列表(即也拥有TeamID == teamId的那个)?
谢谢。
您需要从表示视图中所需内容的视图模型开始
public class TeamVM
{
public string ID { get; set; }
public string Name { get; set; }
public string Captain { get; set; }
public IEnumerable<TournamentVM> Tournaments { get; set; }
}
public class TournamentVM
{
public string ID { get; set; }
public DateTime Date { get; set; }
public string Place { get; set; }
public string Competitor { get; set; }
}
然后查询您的数据库以获得团队(通过其TeamID
),包括Tournament
的集合,并将结果映射到您的视图模型。使用LinqToEntities
Team team = db.Teams.Where(x => x.TeamID == teamId).FirstOrDefault();
if (team == null) { ... }; // error
TeamVM model = new TeamVM
{
ID = team.TeamID,
Name = team.TeamName,
Captain = team.Captain,
// join the collections into a new collection of TournamentVM
Tournaments = team.TeamA.Where(x => x.TeamAID == team.TeamID).Select(x => new TournamentVM
{
ID = x.TournamentID,
Date = x.TournamentDate,
Place = x.Place,
Competitor = x.TeamB.TeamName
}).Concat(team.TeamB.Where(x => x.TeamBID == team.TeamID).Select(x => new TournamentVM
{
ID = x.TournamentID,
Date = x.TournamentDate,
Place = x.Place,
Competitor = x.TeamA.TeamName
})).OrderBy(x => x.Date)
};
return View(model);
并在视图中
@model TeamVM
....
<div>@Model.ID</div>
....
<table>
<thead> ... </thead>
<tbody>
@foreach(var item in Model.Tournaments)
{
<tr>
<td>@item.ID</td>
<td>@item.Date</td>
....
</tr>
}
</tbody>
</table>
以上是关于详细信息使用自定义模型通过LINQ查看相关数据的主要内容,如果未能解决你的问题,请参考以下文章