How to know if I Won the lottery In C# [closed] - c#
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
So, I'm trying to make something to check if won the lottery automatically.
This game i'm playing has 25 numbers, you can choose 15.
if you hit 15 numbers, you win! (it doesn't need to be in the sequence)
I have a list with all the games called: 'TodosJogos'
and my gamble called: 'Jogado'
Now, I'm doing in this way: (which I think it's a terrible one)
private void VerificaVitoria(Jogo Jogado, string TipoJogo)
{
bool Ganhou;
List<Jogo> LstClassificadas = new List<Jogo>();
LstClassificadas = TodosJogos.Where(x => x.N1 == Jogado.N1 ||
x.N1 == Jogado.N2 ||
x.N1 == Jogado.N3 ||
x.N1 == Jogado.N4 ||
x.N1 == Jogado.N5 ||
x.N1 == Jogado.N6 ||
x.N1 == Jogado.N7 ||
x.N1 == Jogado.N8 ||
x.N1 == Jogado.N9 ||
x.N1 == Jogado.N10 ||
x.N1 == Jogado.N11 ||
x.N1 == Jogado.N12 ||
x.N1 == Jogado.N13 ||
x.N1 == Jogado.N14 ||
x.N1 == Jogado.N15).ToList<Jogo>();
LstClassificadas = LstClassificadas.Where(x => x.N2 == Jogado.N1 ||
x.N2 == Jogado.N2 ||
x.N2 == Jogado.N3 ||
x.N2 == Jogado.N4 ||
x.N2 == Jogado.N5 ||
x.N2 == Jogado.N6 ||
x.N2 == Jogado.N7 ||
x.N2 == Jogado.N8 ||
x.N2 == Jogado.N9 ||
x.N2 == Jogado.N10 ||
x.N2 == Jogado.N11 ||
x.N2 == Jogado.N12 ||
x.N2 == Jogado.N13 ||
x.N2 == Jogado.N14 ||
x.N2 == Jogado.N15).ToList<Jogo>();
LstClassificadas = LstClassificadas.Where(x => x.N3 == Jogado.N1 ||
x.N3 == Jogado.N2 ||
x.N3 == Jogado.N3 ||
x.N3 == Jogado.N4 ||
x.N3 == Jogado.N5 ||
x.N3 == Jogado.N6 ||
x.N3 == Jogado.N7 ||
x.N3 == Jogado.N8 ||
x.N3 == Jogado.N9 ||
x.N3 == Jogado.N10 ||
x.N3 == Jogado.N11 ||
x.N3 == Jogado.N12 ||
x.N3 == Jogado.N13 ||
x.N3 == Jogado.N14 ||
x.N3 == Jogado.N15).ToList<Jogo>();
//.....
Ganhou = LstClassificadas.Count > 0 ? true : false;
if (Ganhou)
{
MessageBox.Show("You won in the game: " + TipoJogo);
}
}
It's the similar to this query:
select * from jogos
where n1 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n2 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n3 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n4 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n5 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n6 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n7 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n8 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n9 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n10 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n11 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n12 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n13 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n14 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n15 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
Is there any better way to do it?
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.
:-)
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.
Related
Store math operator in variable
I am creating an application in C# MVVM. I have a simple question. Is there any possibility to store math operator in variable? I have a code like that: public ICollectionView FilteredCollection { get { return filteredCollection; } set { filteredCollection = value; OnPropertyChanged("FilteredCollection"); } } FilteredCollection.Filter = x => ( (string.IsNullOrEmpty(DynamicSearchEmployeeName) || ((Employee)x).EmployeeName.Contains(DynamicSearchEmployeeName)) && (DynamicSearchEmployeeID == null || ((Employee)x).EmployeeID == DynamicSearchEmployeeID) && (string.IsNullOrEmpty(DynamicSearchEmployeeSalary) || ((Employee)x).EmployeeSalary == Convert.ToInt32(DynamicSearchEmployeeSalary)) && (string.IsNullOrEmpty(DynamicSearchEmployeeDesigner) || ((Employee)x).EmployeeName.Contains(DynamicSearchEmployeeDesigner)) && (string.IsNullOrEmpty(DynamicSearchEmployeeEmailID) || ((Employee)x).EmployeeName.Contains(DynamicSearchEmployeeEmailID)) ); What I want to achieve: In fourth line (DynamicSearchEmployeeSalary) math operator should be depended on following conditions: if (IsPressedEqual == true) VARIABLE = "==" if (IsPressedLess == true) VARIABLE = "<" if (IsPressedGreater == true) VARIABLE = ">" if (IsPressedLess == true && IsPressedEqual == true) VARIABLE = "<=" if (IsPressedGreater == true && IsPressedEqual == true) VARIABLE = ">=" Scenario: For example I put a value like 10000 in textbox, then click on button with "=" operator. As a result I want to receive Employees with Salary equals than 10000. Then I click on ">". And I have Employees with Salary greater and equals 10000. FilteredCollection.Filter = x => ( (string.IsNullOrEmpty(DynamicSearchEmployeeName) || ((Employee)x).EmployeeName.Contains(DynamicSearchEmployeeName)) && (DynamicSearchEmployeeID == null || ((Employee)x).EmployeeID == DynamicSearchEmployeeID) && (string.IsNullOrEmpty(DynamicSearchEmployeeSalary) || ((Employee)x).EmployeeSalary VARIABLE Convert.ToInt32(DynamicSearchEmployeeSalary)) && (string.IsNullOrEmpty(DynamicSearchEmployeeDesigner) || ((Employee)x).EmployeeName.Contains(DynamicSearchEmployeeDesigner)) && (string.IsNullOrEmpty(DynamicSearchEmployeeEmailID) || ((Employee)x).EmployeeName.Contains(DynamicSearchEmployeeEmailID))
I've made a simple Rule Based Engine ... I think it will help you in your issue ... please find it as a nuget package here: https://www.nuget.org/packages/IbnSherien.RuleBasedEngine/ you can create a rule like this: var rule = RuleEngine.CreateRule<Employee>() .If<Employee>(e => e.EmployeeSalary).GreaterThan(DynamicSearchEmployeeSalary) .Validate(); FilteredCollection.Filter = x => ( (string.IsNullOrEmpty(DynamicSearchEmployeeName) || ((Employee)x).EmployeeName.Contains(DynamicSearchEmployeeName)) && (DynamicSearchEmployeeID == null || ((Employee)x).EmployeeID == DynamicSearchEmployeeID) && (string.IsNullOrEmpty(DynamicSearchEmployeeSalary) || rule.Match((Employee)x).IsMatch) && (string.IsNullOrEmpty(DynamicSearchEmployeeDesigner) || ((Employee)x).EmployeeName.Contains(DynamicSearchEmployeeDesigner)) && (string.IsNullOrEmpty(DynamicSearchEmployeeEmailID) || ((Employee)x).EmployeeName.Contains(DynamicSearchEmployeeEmailID)) feel free to add any comments or contribute to the package
c# Multiple Independent Filters on Lambda Query
I've been working on this for hours today and feel like there is an easy way to do this but I am unable to make it work by anything but brute force. I have an entity in my application that serves as a Mapping between two objects, based on 5 filters. The goal is to find the record with the most specific match to the filters. Right now, I am manually brute forcing 2^5 queries to get the most specific row, but feel like there has to be a much easier way to do this. The only gotcha here is that there may be no match in the database for a specific filter (or all), in which case I want to select the NULL record. Below I have an excerpt of my embarrassing brute force query -- I first want to try and match on all 5 filters, then on permutations of 4 matching, then 3, then 2, then 1, and finally all Nulls. incList.FirstOrDefault(x => x.Filter1 == filter1Parameter && x.Filter2 == filter2Parameter && x.Filter3 == filter3Parameter && x.Filter4 == filter4Parameter && x.Filter5 == filter5Parameter || x.Filter1 == null && x.Filter2 == filter2Parameter && x.Filter3 == filter3Parameter && x.Filter4 == filter4Parameter && x.Filter5 == filter5Parameter || x.Filter1 == filter1Parameter && x.Filter2 == null && x.Filter3 == filter3Parameter && x.Filter4 == filter4Parameter && x.Filter5 == filter5Parameter || x.Filter1 == filter1Parameter && x.Filter2 == filter2Parameter && x.Filter3 == null && x.Filter4 == filter4Parameter && x.Filter5 == filter5Parameter || x.Filter1 == filter1Parameter && x.Filter2 == filter2Parameter && x.Filter3 == filter3Parameter && x.Filter4 == null && x.Filter5 == filter5Parameter || x.Filter1 == filter1Parameter && x.Filter2 == filter2Parameter && x.Filter3 == filter3Parameter && x.Filter4 == filter4Parameter && x.Filter5 == null I originally thought I could have a simple statement that would independently grab the value if present, and grab null if not incList.FirstOrDefault(x => (x.Filter1 == filter1Parameter || x.Filter1 == null) && (x.Filter2 == filter2Parameter || x.Filter2 == null) && (x.Filter3 == filter3Parameter || x.Filter3 == null) && (x.Filter4 == filter4Parameter || x.Filter4 == null) && (x.Filter5 == filter5Parameter || x.Filter5 == null)); But that did not work. Any pointers would be appreciated.
I'm still trying to understand the full extent of the requirement. However, have you explored abstracting this Func or Func(s) in a separate class. This separate class or classes would operate like a Strategy. Only in charge of what filters a collection based on a certain predicate. If that does not seem like a good route, what about looking into writing your own implementation IEqualityComparer. This will allow you determine what makes these objects equal.
You can introduce something like a "fitness function" and then select item with max fit value var bestMatch = incList.Select(x => new { item = x, fit = (x.Filter1 == null ? 1 : (x.Filter1 == filter1Parameter ? 2 : 0)) * (x.Filter2 == null ? 1 : (x.Filter2 == filter2Parameter ? 2 : 0)) // and so on }) .OrderByDescending(x => x.fit) .FirstOrDefault(x => x.fit > 0)?.item;
prevent shortcuts with wpf webbrowser
hey I have a problem with my wpf webbrowser. I dont want that you can press shortcuts like "CRTL + N" for a new tab for example. I already found out how to do it, but if I want to handle more shortcuts it will only prevent the last one. I know that this will be very simple but I dont know how to fix it at the moment. Here is my code: e.Handled = e.Key == Key.N && e.KeyboardDevice.Modifiers == ModifierKeys.Control; e.Handled = e.Key == Key.O && e.KeyboardDevice.Modifiers == ModifierKeys.Control; e.Handled = e.Key == Key.OemMinus && e.KeyboardDevice.Modifiers == ModifierKeys.Control; e.Handled = e.Key == Key.OemPlus && e.KeyboardDevice.Modifiers == ModifierKeys.Control; e.Handled = e.Key == Key.Subtract && e.KeyboardDevice.Modifiers == ModifierKeys.Control; e.Handled = e.Key == Key.Add && e.KeyboardDevice.Modifiers == ModifierKeys.Control;
You need to OR together your conditions. e.Handled = ((e.Key == Key.N) && (e.KeyboardDevice.Modifiers == ModifierKeys.Control)) || ((e.Key == Key.O) && (e.KeyboardDevice.Modifiers == ModifierKeys.Control)) || ((e.Key == Key.OemMinus) && (e.KeyboardDevice.Modifiers == ModifierKeys.Control)) || ((e.Key == Key.OemPlus) && (e.KeyboardDevice.Modifiers == ModifierKeys.Control)) || ((e.Key == Key.Subtract) && (e.KeyboardDevice.Modifiers == ModifierKeys.Control)) || ((e.Key == Key.Add) && (e.KeyboardDevice.Modifiers == ModifierKeys.Control)); As Modifier CTRL appears to be common, this can be separated out from the keys & the simplified code would be something like e.Handled = (e.KeyboardDevice.Modifiers == ModifierKeys.Control) && ((e.Key == Key.N) || (e.Key == Key.O) || (e.Key == Key.OemMinus) || ...... ) Note that I have added brackets that some people will say are unnecessary, but I prefer them for readability.
Condensing an if-elseif-else statement to save on comparisons
I was hoping to get some help with this. The only real issue I'm having is that this code is not as efficient as it could be. Basically I have a set of tiles and need to decide what kind of tiles to instantiate based on the combination of tiles around them. In the code below, the position of the tile I'm checking is the key in my map (ex map[right] is the tile to the right, topLeft is the tile adjacent diagonally to the top left and top2 etc. is the tile two spaces in that direction). The code in question: if (map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.NOTHING && map[bottom].Type == TileType.NOTHING) { Instantiate(wallEdgeTopLeftInternalCornerTile, t.Position, Quaternion.identity); } else if (map[left].Type == TileType.NOTHING && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.NOTHING) { Instantiate(wallEdgeTopRightInternalCornerTile, t.Position, Quaternion.identity); } else if (map[left].Type == TileType.WALL && map[top].Type == TileType.NOTHING && map[right].Type == TileType.NOTHING && map[bottom].Type == TileType.WALL) { Instantiate(wallEdgeBottomLeftInternalCornerTile, t.Position, Quaternion.identity); } else if (map[left].Type == TileType.NOTHING && map[top].Type == TileType.NOTHING && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL) { Instantiate(wallEdgeBottomRightInternalCornerTile, t.Position, Quaternion.identity); } else if (map[left].Type == TileType.WALL && map[top].Type == TileType.FLOOR && map[right].Type == TileType.FLOOR && map[bottom].Type == TileType.WALL) { Instantiate(wallEdgeCornerTopRightTile, t.Position, Quaternion.identity); } else if (map[left].Type == TileType.FLOOR && map[top].Type == TileType.FLOOR && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL) { Instantiate(wallEdgeCornerTopLeftTile, t.Position, Quaternion.identity); } else if (map[left].Type == TileType.FLOOR && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[topRight].Type == TileType.NOTHING) { Instantiate(wallEdgeCornerBottomLeftTile, t.Position, Quaternion.identity); } else if (map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.FLOOR && map[bottom].Type == TileType.WALL && map[topLeft].Type == TileType.NOTHING) { Instantiate(wallEdgeCornerBottomRightTile, t.Position, Quaternion.identity); } else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[top2].Type == TileType.NOTHING && map[left2].Type == TileType.NOTHING) || (map[left].Type == TileType.FLOOR && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[top2].Type == TileType.WALL && map[left2].Type == TileType.FLOOR && map[bottom2].Type == TileType.WALL)) { Instantiate(wallTopLeftTile, t.Position, Quaternion.identity); } else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[top2].Type == TileType.NOTHING && map[right2].Type == TileType.NOTHING) || (map[right].Type == TileType.FLOOR && map[top].Type == TileType.WALL && map[left].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[top2].Type == TileType.WALL && map[right2].Type == TileType.FLOOR && map[bottom2].Type == TileType.WALL)) { Instantiate(wallTopRightTile, t.Position, Quaternion.identity); } else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.FLOOR && map[top2].Type == TileType.WALL && map[left2].Type == TileType.NOTHING) || (map[left].Type == TileType.FLOOR && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.FLOOR && map[top2].Type == TileType.WALL && map[left2].Type == TileType.FLOOR && map[bottom2].Type == TileType.FLOOR)) { Instantiate(wallBottomLeftTile, t.Position, Quaternion.identity); } else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.FLOOR && map[top2].Type == TileType.WALL && map[right2].Type == TileType.NOTHING) || (map[right].Type == TileType.FLOOR && map[top].Type == TileType.WALL && map[left].Type == TileType.WALL && map[bottom].Type == TileType.FLOOR && map[top2].Type == TileType.WALL && map[right2].Type == TileType.FLOOR && map[bottom2].Type == TileType.FLOOR)) { Instantiate(wallBottomRightTile, t.Position, Quaternion.identity); } else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[left2].Type == TileType.NOTHING && map[bottom2].Type == TileType.FLOOR) || (map[left].Type == TileType.FLOOR && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[left2].Type == TileType.FLOOR && map[bottom2].Type == TileType.FLOOR)) { Instantiate(wallMidLeftTile, t.Position, Quaternion.identity); } else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[right2].Type == TileType.NOTHING && map[bottom2].Type == TileType.FLOOR) || (map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.FLOOR && map[bottom].Type == TileType.WALL && map[right2].Type == TileType.FLOOR && map[bottom2].Type == TileType.FLOOR)) { Instantiate(wallMidRightTile, t.Position, Quaternion.identity); } else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[top2].Type == TileType.NOTHING && map[bottom2].Type == TileType.WALL)) { Instantiate(wallTopTile, t.Position, Quaternion.identity); } else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.FLOOR && map[top2].Type == TileType.WALL && map[bottom2].Type == TileType.FLOOR)) { Instantiate(wallBottomTile, t.Position, Quaternion.identity); } else if (map[top].Type == TileType.NOTHING && map[bottom].Type == TileType.WALL) { Instantiate(wallEdgeBottomTile, t.Position, Quaternion.identity); } else if ((map[left].Type == TileType.NOTHING && map[right].Type == TileType.FLOOR) || (map[left].Type == TileType.NOTHING && map[right].Type == TileType.WALL && map[top].Type != TileType.NOTHING)) { Instantiate(wallEdgeRightTile, t.Position, Quaternion.identity); } else if ((map[left].Type == TileType.FLOOR && map[right].Type == TileType.NOTHING) || (map[left].Type == TileType.WALL && map[right].Type == TileType.NOTHING && map[top].Type != TileType.NOTHING)) { Instantiate(wallEdgeLeftTile, t.Position, Quaternion.identity); } else if (map[bottom].Type == TileType.NOTHING && map[top].Type == TileType.FLOOR) { Instantiate(wallEdgeTopTile, t.Position, Quaternion.identity); } else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[top2].Type == TileType.WALL && map[bottom2].Type == TileType.FLOOR)) { Instantiate(wallMiddleTile, t.Position, Quaternion.identity); } else { // Should never get here, so if a white Tile is seen, something went wrong! Instantiate(whiteTile, t.Position, Quaternion.identity); } I had made a table because I thought that I would instead check each position, assign it a number from 0-2, convert that number from base 3 to decimal, then use a switch statement on the outcome to determine which tile to instantiate. The number codes are on the very right. But there are so many combinations, I feel like there has to be a better or easier way. Any ideas at all are appreciated! Table: Table of conditions
To simply reduce the number of caparison operations, I would suggest arrange your AND conditionals in a tree-type fashion. here is a simplified example. note: I'm using single & for readability- I mean logical AND. starting with: if(a&b&c) {functionW(a);return;} //1 comparison, 2 ANDops else if(a&b&!c) {functionX(a);return;}//2 comparison, 4 ANDops else if(a&!b&!c) {functionX(b);return;}//3 comparison, 6 ANDops else if(a&!b&c) {functionX(c);return;}//4 comparison, 8 ANDops else if(!&a&b&c) {functionY(b);return}//5 comparison, 10 ANDops else if(!a&!b&c){functionZ(c);return}//6 comparison, 12 ANDops else if(!b&!e){functionZ(c);return;}//7 comparison, 13 ANDops Tree-format: take a branch for each element we are checking, so first check for a: //out here,first, go all the statements, where it doesn't mater what value a has. We haveonly one condition in our example, but if we had many, we could create a separate "tree" out here. if(!b&!e){functionZ(c);return;}//1 comparison, 1 ANDops //now we start a branch based on a if(a) {//in here go all the if statements where a is true //now we create onther tree branch based on b if(b) { if(!c){ functionW(a); return;}// total 4 comparisons to get here. 1 ANDops } else {//all option where b is false if(c) {functionX(c); return;}// total 4 comparisons to get here, 1 ANDops else {functionX(b); return;}// total 4 comparisons to get here, 1 ANDops } } else {//in here go all the if statements where a is false (there are only two, so no need for a branch) if(b&c) {functionY(b);return}// total 3 comparisons to get here. 2 ANDops else if(!b&c){functionZ(c);return}// total 3 comparisons to get here, 3 ANDops } Obviously you don't get a huge advantage with only 7 different conditionals, but this improvement grows, with the amount of conditionals you start with.
List filtering with LINQ
So I came across this method in code: private void FilterBasedUponPermission(List<Data.Indications.SpWeb_SavedIndications1LightDataObject> list) { list.RemoveAll(item => (item.Permission == Controllers.Indications.ICConstants.TradeType_LLH && !isLLH) || (item.Permission == Controllers.Indications.ICConstants.TradeType_ALM && !isALM) || (item.Permission == Controllers.Indications.ICConstants.TradeType_RealEstate && !isRE) || (item.Permission == Controllers.Indications.ICConstants.TradeType_Auditor && !isAuditor)); } However, in the list passed in, some of the permission values are null, and it's keeping them in the filtered list. I want it to remove the items that also don't have a permission set, not just the ones that don't match the permission that you have as a user. Thanks!
Just add a item.Permission == null list.RemoveAll(item => item.Permission == null || (item.Permission == Controllers.Indications.ICConstants.TradeType_LLH && !isLLH) || (item.Permission == Controllers.Indications.ICConstants.TradeType_ALM && !isALM) || (item.Permission == Controllers.Indications.ICConstants.TradeType_RealEstate && !isRE) || (item.Permission == Controllers.Indications.ICConstants.TradeType_Auditor && !isAuditor));
private void FilterBasedUponPermission(List<Data.Indications.SpWeb_SavedIndications1LightDataObject> list) { list.RemoveAll(item => (item.Permission == null) || (item.Permission == Controllers.Indications.ICConstants.TradeType_LLH && !isLLH) || (item.Permission == Controllers.Indications.ICConstants.TradeType_ALM && !isALM) || (item.Permission == Controllers.Indications.ICConstants.TradeType_RealEstate && !isRE) || (item.Permission == Controllers.Indications.ICConstants.TradeType_Auditor && !isAuditor)); }
Does on of those work? list.RemoveAll(item => !item.Permission.HasValue); // in case Permission is Nullable<T> list.RemoveAll(item => item.Permission == null); You can add all the other conditions after a logical or (||). Cheers, Matthias