我有桌子“动作”,其中包含播放器,游戏,类型 - 麦芽等与其他桌子相关的字段。我的c#代码中还有两个列表:
List<Player> playersForStats
List<Game> gamesForStats
现在我想从Actions中选择一行(字段)玩家在列表playersForStats和(字段)游戏列表gamesForStats中。我怎样才能使用linq?我必须使用内连接或其他东西吗?
答案 0 :(得分:1)
如果您的对象具有正确的equals方法,那么这样的东西应该有效:
var oActions = ..
var oData = oActions.Where(c=> c.Players.TrueForAll(p => playersForStats.Contains(p)) && gamesForStats.Contains(c.Game));
假设c.Players
为List
且c.Game
为字段。
答案 1 :(得分:1)
我假设你的行动清单名为行动:
actions.Where(x=>playersForStats.Contains(x.Player) && gamesForStats.Contains(x=>x.Game));
实际上只需要搜索playerForStats和gamesForStats是否包含相关动作的玩家和游戏。
答案 2 :(得分:0)
这样的事情:
class Program
{
static void Main(string[] args)
{
List<Action> actions = new List<Action>();
List<Game> gamesForStats = new List<Game>();
List<Player> playersForStats = new List<Player>();
List<Action> result = (from oAction in actions
join game in gamesForStats on oAction.Game equals game.GameName
join player in playersForStats on oAction.Player equals player.PlayerName
select oAction).ToList();
Console.ReadLine();
}
}
public class Player
{
public string PlayerName { get; set; }
}
public class Game
{
public string GameName { get; set; }
}
public class Action
{
public string Player { get; set; }
public string Game { get; set; }
public string Type { get; set; }
}