如何检查ID是否与另一个ID匹配,然后通过foreach显示它们

时间:2012-06-06 09:35:55

标签: c# database xna foreach

不确定如何说出这个问题,但无论如何我都会尝试。

我目前正在开展一个项目,我希望通过该项目提取用户的高分。我的数据库中的高分ID,看起来像这样:

The user database:
|ID | Username |
| 1 | Test1    |
| 2 | Test2    |
| 3 | Test3    |

The highscore database:
|ID |Highscore |HighscoreID|
| 1 | 200      | 1         |
| 1 | 230      | 2         |
| 1 | 240      | 3         |

首先,我想检查用户的ID是否与高分数据库中的ID匹配(HighscoreID),这样当显示所有高分时,Test1(谁的ID为1)将显示在高分数的前面哪个ID与HighscoreID中的id对应。有点像;

If(Username.ID == 1 && Highscore.HighscoreID == 1)
{
 //Display The username + the score associated with that username
}

我不确定如何解释这一点,但是当我们想要通过网站上的ID显示某些内容时,我们会在地址链接中显示ID,然后从地址链接中取出所有ID

我希望这个解释得很好,很快就会在游戏中达到最后期限,所以我希望我不必重写这个问题。

先谢谢你,你们摇滚。

编辑

基本上这个:

 MathAndYouDBEntities db = new MathAndYouDBEntities();
            User users = new User();
            HighScore highscor = new HighScore();
            int idCounter = 0;
            for (int i = 1; i < 5; i++)
            {
                i = idCounter;
                if (users.ID == i && highscor.HighscoreID == i)
                { 

                }
            }

如果我想循环遍历所有这些,以便我以后可以显示它们?

d

oing this basicly crashes the program:
   MathAndYouDBEntities db = new MathAndYouDBEntities();
            User users = new User();
            HighScore highscor = new HighScore();
            int idCounter = 0;
            for (int i = 1; i < 5; i++)
            {
                i = idCounter;
                if (users.ID == idCounter && highscor.HighscoreID == idCounter)
                {
                    allUsers += users.Bruger.ToString();
                    highscores += highscor.UserHighscore.ToString() + '\n';
                }
            }

            message = allUsers + " " + highscores;

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

     If(Username.ID == 1 && Highscore.HighscoreID == 1)
     {
       System.out.println("Username is: " Username.name + "And the high score is :" + Highscore.Highscore);
       //Display The username + the score associated with that username
     }

因为如果您使用正确的高分来检查正确的用户名ID,您可以使用相同的高分来获得高分数。

我希望我有意义!

编辑:对不起,我没有在for循环中读到你想要它

  for(Highscore h : HighscoreList)   //ifyourusinganarraylistnamedHighscoreList
   If(Username.ID == 1 && Highscore.HighscoreID == 1)
   {
            System.out.println("Username is: " Username.name + "And the high score is    :" + Highscore.Highscore);
   }

答案 1 :(得分:1)

如果你使用某种ORM,你可以这样做。

var highScore = Highscores.Where(c => c.HighscoreID == 1)
                          .Max(c => c.Highscore);

或者在查询字符串中执行此操作。

答案 2 :(得分:0)

经过大量测试后找到答案。

MathAndYouDBEntities db = new MathAndYouDBEntities();

            var queryResults =
                (
                from u in db.Users
                join h in db.HighScores
                on u.ID equals h.HighscoreID
                select new { u.Bruger, h.UserHighscore }
                );

            foreach (var h in queryResults)
            {
                allUsers += h.Bruger.ToString();
                highscores += h.UserHighscore.ToString();
            }

感谢大家的帮助。