简体   繁体   English

比较asp.net中的两个表

[英]Compare two tables in asp.net

I'm making a website to show various statistics regarding games. 我正在制作一个网站,以显示有关游戏的各种统计信息。

I got two seperate tables in an sql-database. 我在sql数据库中有两个单独的表。 One called PlayerMatch and another called Match. 一个叫做PlayerMatch,另一个叫做Match。

The two columns I want to compare is TeamId in PlayerMatch (PlayerMatch.TeamId vs. WinningTeamId in Match (Match.WinningTeamId) 我要比较的两列是PlayerMatch中的TeamId(PlayerMatch.TeamId与Match中的WinningTeamId(Match.WinningTeamId)

To show it all, I have made a table, where I have placed some asp:repeaters inside. 为了显示所有内容,我制作了一张桌子,在其中放置了一些asp:repeaters。 Here is one of them, as they are all essentially the same. 这是其中之一,因为它们基本上是相同的。

<asp:Repeater ID="repHighGPM" runat="server">
     <ItemTemplate>
         <td><a href="Match.aspx?id=<%#Eval("Match.MatchNumber") %>"><%#Eval("Match.MatchNumber") %></a></td>
          <td><%#Eval("Match.WinningTeamId") %></td>
          <td><%#Eval("Name") %></td>
          <td>
               <img src=' <%#Eval("Image") %>' /></td>
          <td><%#Eval("GoldPerMinute") %></td>
      </ItemTemplate>
</asp:Repeater>

Now, the problem is that as it is now it will only show the Id of the team who won (Match.WinningTeamId). 现在,问题在于,它仅会显示获胜团队的ID(Match.WinningTeamId)。 But not if the player was on that team. 但是如果球员在那支球队中,那就不是。 So essentially it just needs to check if PlayerMatch.TeamId == Match.WinningTeamId. 因此,从本质上讲,它只需要检查PlayerMatch.TeamId == Match.WinningTeamId。 And if that is true it should write "won match", while false will be "lost match" 如果为true,则应写“ won match”,而为false则是“ lost match”

Any ideas? 有任何想法吗? I know it should be some kind of if-else function, but I have no idea how to do it. 我知道它应该是某种if-else函数,但是我不知道该怎么做。

You could use the inner join in sql query : make a datasource to your databse write the query after that add the datasource to repeater DataSourceID="" .... 您可以在sql查询中使用内部联接:将数据源添加到您的数据库中,然后将数据源添加到转发器DataSourceID =“”...。

<asp:Repeater ID="repHighGPM" runat="server" DataSourceID="match">
     <ItemTemplate>
         <td><a href="Match.aspx?id=<%#Eval("Match.MatchNumber") %>"><%#Eval("Match.MatchNumber") %></a></td>
          <td><%#Eval("Match.WinningTeamId") %></td>
          <td><%#Eval("Name") %></td>
          <td>
               <img src=' <%#Eval("Image") %>' /></td>
          <td><%#Eval("GoldPerMinute") %></td>
      </ItemTemplate>
</asp:Repeater>    



    <asp:SqlDataSource ID="match" runat="server" ConnectionString="<%$ ConnectionStrings:database %>" SelectCommand="select m.match number, m.winningTeamID, Name ,Image from Match m inner join
        playermatch p on p.Teamid = m.WinningTeamId"></asp:SqlDataSource>

for more information about inner join read this article : http://www.w3schools.com/sql/sql_join_inner.asp 有关内部联接的更多信息,请阅读本文: http : //www.w3schools.com/sql/sql_join_inner.asp

I have solved the problem with the following solution: 我已通过以下解决方案解决了该问题:

    public string MatchResult (object Result) {
    int id = Convert.ToInt32(Request.QueryString["id"]);
    DOTA2DataContext db = new DOTA2DataContext();
    var Victory = Result;
    var TeamId = db.PlayerMatches.Where(x => x.TeamId == id);
    var WinningTeam = db.Matches.Where(x => x.WinningTeamId == id);
    {
        if (TeamId == WinningTeam)
        { 
            return "Won match";
        }
        else
        {
            return "Lost match";
        }

    }
}

And the repeater. 和中继器。

<asp:Repeater ID="repHighD" runat="server">
                        <ItemTemplate>
                            <td><a href="Match.aspx?id=<%#Eval("Match.MatchNumber") %>"><%#Eval("Match.MatchNumber") %></a></td>
                            <td><%# MatchResult(Eval("Match.WinningTeamId"))%></td>
                            <td><%#Eval("Hero.Name") %></td>
                            <td>
                                <img src=' <%#Eval("Hero.Image") %>' /></td>
                            <td><%#Eval("Denies") %></td>
                        </ItemTemplate>
                    </asp:Repeater>    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM