Where inside where in linq

I am trying to solve the following problem with linq (already solved this with normal programming):

http://projecteuler.net/problem=43

I currently have the following:

class Program { static void Main(string[] args) { var range0 = Enumerable.Range(0, 3); var range1 = Enumerable.Range(0, 3); var range2 = Enumerable.Range(0, 3); var combo = from val0 in range0 from val1 in range1 from val2 in range2 where (val0 + val1 + val2 == 3) select new { value = val0.ToString() + val1.ToString() + val2.ToString() }; foreach( var value in combo ) { Console.WriteLine(value.value); } Console.ReadLine(); } } 

Ultimately, I want to expand this query to accept 9 values, but for now, to my question, how can I check the where clause if each value is different? val0 val1 and val3 must be different.

+4
source share
3 answers

What about

 where new List<int> { val0, val1, val2 }.Distinct().Count() == 3 && (val0 + val1 + val2 == 2) 
+7
source

Put the values ​​in the list, get different values ​​and check how many items you have.

 var combo = from val0 in range0 from val1 in range1 from val2 in range2 let values = new List<int>() {val0, val1, val2} where (val0 + val1 + val2 == 2) && values.Distinct().Count() == 3 select new { value = val0.ToString() + val1.ToString() + val2.ToString() }; 
+1
source
  from val0 in range0 from val1 in range1 where val1 != val0 from val2 in range2 where val2 != val1 && val2 != val0 && (val0 + val1 + val2 == 2) select new { value = val0.ToString() + val1.ToString() + val2.ToString() }; 
0
source

All Articles