详细'使用自定义模型通过LINQ查看相关数据

时间:2018-03-01 18:36:41

标签: c# asp.net-mvc linq entity-framework-6

由于我是ASP的新手,确实很困惑如何通过LINQ派生的模型/表的某些特定列的 List 反映在相关的父项目上详细信息页面。 考虑两个类的示例,如:

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&amp;正在使用相关的ForeignKeys。

主要对象将在顶部显示一个团队详细信息的一部分,但相同的锦标赛相关条目列在下面。

由于团队(例如Team_1)可能存在于锦标赛的TeamA列中,甚至存在于TeamB列中,因此会出现如下问题:如何以下列方式呈现相同的内容:

TeamID:ID_1

TeamName:Team_1

TeamCaptain:Captain1

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());
    }

因此,要将相同的列表连接起来,但要使用Team ID&amp;名称翻转,导致在名为 TeamLinks 的属性中编译的所有ID,而竞争对手的名称在 TeamNames 中排列。

现在,可以通过将@model IEnumerable 作为指定如何在团队的详细信息下显示此类列表的主要目标来呈现所述列表(即拥有TeamID == teamId的人?

感谢。

1 个答案:

答案 0 :(得分:1)

您需要从一个代表您在视图中所需内容的视图模型开始

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>