score:3

Accepted answer

Judging from your SQL query, I think this is what you want:

var lotteryNumbers = new int[] { ... };
var results =
    from j in jogos
    where lotteryNumbers.Contains(j.n1) &&
          lotteryNumbers.Contains(j.n2) &&
          lotteryNumbers.Contains(j.n3) &&
          lotteryNumbers.Contains(j.n4) &&
          lotteryNumbers.Contains(j.n5) &&
          lotteryNumbers.Contains(j.n6) &&
          lotteryNumbers.Contains(j.n7) &&
          lotteryNumbers.Contains(j.n8) &&
          lotteryNumbers.Contains(j.n9) &&
          lotteryNumbers.Contains(j.n10) &&
          lotteryNumbers.Contains(j.n11) &&
          lotteryNumbers.Contains(j.n12) &&
          lotteryNumbers.Contains(j.n13) &&
          lotteryNumbers.Contains(j.n14) &&
          lotteryNumbers.Contains(j.n15)
    select j;

Or in fluent syntax

var results =
    jogos.Where(j =>
          lotteryNumbers.Contains(j.n1) &&
          lotteryNumbers.Contains(j.n2) &&
          lotteryNumbers.Contains(j.n3) &&
          lotteryNumbers.Contains(j.n4) &&
          lotteryNumbers.Contains(j.n5) &&
          lotteryNumbers.Contains(j.n6) &&
          lotteryNumbers.Contains(j.n7) &&
          lotteryNumbers.Contains(j.n8) &&
          lotteryNumbers.Contains(j.n9) &&
          lotteryNumbers.Contains(j.n10) &&
          lotteryNumbers.Contains(j.n11) &&
          lotteryNumbers.Contains(j.n12) &&
          lotteryNumbers.Contains(j.n13) &&
          lotteryNumbers.Contains(j.n14) &&
          lotteryNumbers.Contains(j.n15));

Although this assumes you don't have duplicates in either lotteryNumbers or in j.n1 .. j.n15. Otherwise you'd probably get unexpected results.

score:32

Two ways to solve your problem.

Represent a winning combination as a set of fifteen integers:

HashSet<int>

You have a sequence of winning games:

IEnumerable<HashSet<int>>

and a specific HashSet<int>. You wish to know if the specific set exactly matches any of the winning sets.

Method One

static bool DidIWin(IEnumerable<HashSet<int>> winningNumbers, HashSet<int> myNumbers)
{
    return winningNumbers
      .Where(winningNumber => myNumbers.SetEquals(winningNumber))
      .Any();
}

Or even

static bool DidIWin(IEnumerable<HashSet<int>> winningNumbers, HashSet<int> myNumbers)
{
    return winningNumbers
      .Any(winningNumber => myNumbers.SetEquals(winningNumber));
}

Method Two

static bool DidIWin(IEnumerable<HashSet<int>> winningNumbers, HashSet<int> myNumbers)
{
    return false;
}

Method two is a lot faster. However, it gives the incorrect result on average one time out of every three million winning numbers. This demonstrates that sometimes you can get a big performance win by being willing to accept a tiny amount of inaccuracy.

:-)


Related Articles