代码之家  ›  专栏  ›  技术社区  ›  peirix

linq到sql不计算所有列

  •  0
  • peirix  · 技术社区  · 14 年前

    我有一张桌子叫 Game ,其中包含4列玩家id。我还有一个名为 Player . 当我试图统计一个玩家参加过的所有游戏时,我只得到游戏中第一列的玩家 游戏 桌子。

    player.Games.Count();
    

    Blue1 ...

    alt text

    我还尝试创建一个方法来手动选择所有游戏:

    public int GetPlayerGames(int playerID)
    {
        return (from game in db.Games
                where game.Blue1 == playerID
                || game.Blue2 == playerID
                || game.Red1 == playerID
                || game.Red2 == playerID
                select game).Count();
    }
    

    return from player in db.Players
           where GetPlayerGames(player.ID) > 0
           select player;
    

    我得到这个错误:
    Method 'Int32 GetPlayerGames(Int32)' has no supported translation to SQL.

    3 回复  |  直到 14 年前
        1
  •  1
  •   Dave D    14 年前

    如果你已经建立了四段感情,很可能他们都有不同的名字 Games , Games1 , Games2 Games3 . 通过做 player.Games.Count()

    var allGameCounts = player.Games.Count() + player.Games1.Count() + player.Games2.Count() + player.Games3.Count();
    
        2
  •  0
  •   peirix    14 年前

    实际上,我最终使用了上述方法,但只是先将其转换为一个列表:

    return from player in db.Players.ToList()
           where GetPlayerGames(player.ID) > 0
           select player;
    
        3
  •  0
  •   Amy B    14 年前

    如果您想在sql中进行过滤而不加载整个数据库,可以这样做。。。

    from player in db.Players
    where player.Games.Any()
      || player.Games1.Any()
      || player.Games2.Any()
      || player.Games3.Any()
    select player