Console.WriteLine(\ foreach (var n in commonNumbers) { Console.WriteLine(n); } }
Intersect - 2
publicvoid Linq51() {
List products = GetProductList(); List customers = GetCustomerList();
var productFirstChars = from p in products select p.ProductName[0]; var customerFirstChars = from c in customers
select c.CompanyName[0];
var commonFirstChars = productFirstChars.Intersect(customerFirstChars);
Console.WriteLine(\ foreach (var ch in commonFirstChars) { Console.WriteLine(ch); } }
Except - 1
public void Linq52() {
int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; int[] numbersB = { 1, 3, 5, 7, 8 };
IEnumerable
Except - 2
public void Linq53() {
List products = GetProductList(); List customers = GetCustomerList(); var productFirstChars = from p in products
select p.ProductName[0]; var customerFirstChars =
from c in customers
select c.CompanyName[0];
var productOnlyFirstChars = productFirstChars.Except(customerFirstChars);
Console.WriteLine(\ foreach (var ch in productOnlyFirstChars) { Console.WriteLine(ch); } }
Conversion Operators To Array
public void Linq54() {
double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 }; var sortedDoubles = from d in doubles orderby d descending
select d;
var doublesArray = sortedDoubles.ToArray();
Console.WriteLine(\ for (int d = 0; d < doublesArray.Length; d += 2) { Console.WriteLine(doublesArray[d]); } }
To List
public void Linq55() {
string[] words = { \ var sortedWords = from w in words orderby w
select w;
var wordList = sortedWords.ToList(); Console.WriteLine(\ foreach (var w in wordList) { Console.WriteLine(w); } }
To Dictionary
public void Linq56() {
var scoreRecords = new [] { new {Name = \ new {Name = \ new {Name = \ };
var scoreRecordsDict = scoreRecords.ToDictionary(sr => sr.Name); Console.WriteLine(\}
OfType
public void Linq57() {
object[] numbers = { null, 1.0, \ var doubles = numbers.OfType
Console.WriteLine(\ foreach (var d in doubles) { Console.WriteLine(d); } }
Element Operators First - Simple
public void Linq58() {
List products = GetProductList(); Product product12 = ( from p in products
where p.ProductID == 12 select p ) .First();
ObjectDumper.Write(product12); }
First - Indexed
public void Linq60() {
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
int evenNum = numbers.First((num, index) => (num % 2 == 0) && (index % 2 == 0)); Console.WriteLine(\ }
FirstOrDefault - Simple public void Linq61() { int[] numbers = {};
int firstNumOrDefault = numbers.FirstOrDefault(); Console.WriteLine(firstNumOrDefault); }
FirstOrDefault - Condition
public void Linq62() {
List products = GetProductList();
Product product789 = products.FirstOrDefault(p => p.ProductID == 789);
Console.WriteLine(\ }
FirstOrDefault - Indexed public void Linq63() {
double?[] doubles = { 1.7, 2.3, 4.1, 1.9, 2.9 };
double? num = doubles.FirstOrDefault((n, index) => (n >= index - 0.5 && n <= index + 0.5)); if (num != null)
Console.WriteLine(\ else
Console.WriteLine(\ }
ElementAt
public void Linq64() {
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; int fourthLowNum = ( from n in numbers where n < 5
select n )
.ElementAt(3); // 3 because sequences use 0-based indexing Console.WriteLine(\ }
Generation Operators Range
public void Linq65() {
var numbers =
from n in Sequence.Range(100, 50)
selectnew {Number = n, OddEven = n % 2 == 1 ? \ foreach (var n in numbers) {
Console.WriteLine(\ } }
Repeat
public void Linq66() {
var numbers = Sequence.Repeat(7, 10); foreach (var n in numbers) { Console.WriteLine(n); } }
Quantifiers
Any - Simple
public void Linq67() {
string[] words = { \ bool iAfterE = words.Any(w => w.Contains(\
Console.WriteLine(\ }
Any - Indexed
public void Linq68() {
int[] numbers = { -9, -4, -8, -3, -5, -2, -1, -6, -7 };
bool negativeMatch = numbers.Any((n, index) => n == -index);
Console.WriteLine(\ }
Any - Grouped
public void Linq69() {
List products = GetProductList(); var productGroups = from p in products
group p by p.Category into g
where g.Group.Any(p => p.UnitsInStock == 0) select new {Category = g.Key, Products = g.Group}; ObjectDumper.Write(productGroups, 1); }
All - Simple
public void Linq70() {
int[] numbers = { 1, 11, 3, 19, 41, 65, 19 }; bool onlyOdd = numbers.All(n => n % 2 == 1);
Console.WriteLine(\ }
All - Indexed
public void Linq71() {
int[] lowNumbers = { 1, 11, 3, 19, 41, 65, 19 }; int[] highNumbers = { 7, 19, 42, 22, 45, 79, 24 };
bool allLower = lowNumbers.All((num, index) => num < highNumbers[index]);
Console.WriteLine(\{0}\ }
All - Grouped
public void Linq72() {
List products = GetProductList();