using System; using System.Linq; using System.Collections.Generic; public class Exercise { public static int Main() { var numbers = new List<int>(); numbers.Add(12); numbers.Add(45); numbers.Add(38); numbers.Add(5); numbers.Add(128); numbers.Add(525); numbers.Add(2448); numbers.Add(39); numbers.Add(632); numbers.Add(207); var number = from n in numbers where n % 2 == 0 && n <= 100 select n; foreach (var member in number) Console.WriteLine(member.ToString()); Console.WriteLine(); return 0; } } This would produce:
Once again, remember that the use of parentheses makes it easier to read the code and better understand it: using System; using System.Linq; using System.Collections.Generic; public class Exercise { public static int Main() { var numbers = new List<int>(); numbers.Add(12); numbers.Add(45); numbers.Add(38); numbers.Add(5); numbers.Add(128); numbers.Add(525); numbers.Add(2448); numbers.Add(39); numbers.Add(632); numbers.Add(207); var number = from n in numbers where (n % 2 == 0) && (n <= 100) select n; foreach (var member in number) Console.WriteLine(member.ToString()); Console.WriteLine(); return 0; } } Or better yet: using System; using System.Linq; using System.Collections.Generic; public class Exercise { public static int Main() { var numbers = new List<int>(); numbers.Add(12); numbers.Add(45); numbers.Add(38); numbers.Add(5); numbers.Add(128); numbers.Add(525); numbers.Add(2448); numbers.Add(39); numbers.Add(632); numbers.Add(207); var number = from n in numbers where ((n % 2) == 0) && (n <= 100) select n; foreach (var member in number) Console.WriteLine(member.ToString()); Console.WriteLine(); return 0; } } To negate a conjunction, precede it with a ! operator. Remember that a conjunction is made of two parts. Therefore, if you want to negate only the first part, precede it with !. If you want to negate the whole conjunction, put the conjunction in parentheses but precede it with ! (outside the parentheses).
When creating a LINQ statement, you can use a logical disjunction. You do this with the C#'s OR operator, which is ||.
|
|||||||||||||||||||||