score:3
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.
:-)
Source: stackoverflow.com
Related Articles
- LINQ Source Code Available
- .NET 4 Code Contracts: "requires unproven: source != null"
- How to know if I Won the lottery In C#
- creating Linq to sqlite dbml from DbLinq source code
- source code for LINQ 101 samples
- List or Array of String Contain specific word in Html Source Code
- c# Linq or code to extract groups from a single list of source data
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Value cannot be null. Parameter name: source
- Linq code to select one item
- C# - code to order by a property using the property name as a string
- How do I find the text within a div in the source of a web page using C#
- Roslyn failed to compile code
- Entity-framework code is slow when using Include() many times
- The data source does not support server-side data paging
- How are people unit testing code that uses Linq to SQL
- Entity Framework, Code First and Full Text Search
- What does this C# code with an "arrow" mean and how is it called?
- How to resolve Value cannot be null. Parameter name: source in linq?
- The source contains no DataRows
- Could not find an implementation of the query pattern for source type 'System.Data.Entity.DbSet'
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
- Is there an IEnumerable implementation that only iterates over it's source (e.g. LINQ) once?
- Entity Framework 6 Code First Custom Functions
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Is it possible to express this code in LINQ?
- how to know if my linq query returns null
- I am wondering about the state of connection and impact on code performance by 'yield' while iterating over data reader object
- This code returns distinct values. However, what I want is to return a strongly typed collection as opposed to an anonymous type
- Combine Elements in a List based on Type and Summate their Values, LINQ
- How can I avoid repeating some part of the statement in this LINQ
- Xml file not available to all event methods using XDocument.Load in C#
- Combining information from multiple lists
- how do i handle value when the value is null or Empty using linq query
- How to create an InvocationExpression without parameters
- How can I choose one record over the other?
- How to check the attribute value string from linq in c#
- Guid.ToString() in Linq query?
- LINQ need to parse JSON out of a column
- how to do an inner join and populate property efficiently in Linq?
- How to optimize SQL query generated by Entity Framework in SQL Server Management Studio?
- Querying data using LINQ in C#
- need help translating CTE query into linq (not recursive)
- List order by another contained list
- Handle null ref exceptions in LINQ Expression calls
- Querying a nested list using linq
- C# LINQ group and count questions
- Performance of First() vs Last() on a SortedDictionary
- C# how to convert IEnumerable anonymous lists into data table