我是C#编程的新手。我正在尝试建立一个接受玩家号码和名字的乐透计划(WinForm)。将它们放入ArrayList中。我遇到的麻烦是将彩票号码的数组与ArrayList中每个玩家的数组进行比较。例如:
public class Player
{
public String name;
public Int32[] playerNbrs = new Int32[6];
}
public class Data
{
public static ArrayList arrayPlayers = new ArrayList();
public static int[] lotteryNbrs = new int[6];
}
我知道我应该使用SequenceEqual
但是我无法在ArrayList中提取每个玩家的数组并将其与int[] lotteryNbrs
进行比较!
感谢任何建议!
答案 0 :(得分:1)
您可以使用OfType
方法执行此操作:
// if theWinner is null it means there is no winner
var theWinner = arrayPlayers
.OfType<Player>()
.FirstOrDefault(x => x.playerNbrs.SequenceEqual(lotteryNbrs));
顺便说一句,不要使用ArrayList
。使用强类型集合,例如List<Player>
代替......
修改:如果有多个获胜者:
var theWinners = arrayPlayers
.OfType<Player>()
.Where(x => x.playerNbrs.SequenceEqual(lotteryNbrs));