using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
public class MainClass{
public static void Main(){
int[] numbers = {1, 1, 2, 3, 3};
Console.Write(numbers.Distinct());
}
}
Month: February 2011
Set Operators: Distinct
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class MainClass {
public static void Main() {
int[] fl = { 2, 2, 3, 5, 5 };
var uniqueFactors = fl.Distinct();
Console.WriteLine("Prime factors of 300:");
foreach (var f in uniqueFactors) {
Console.WriteLine(f);
}
}
}
prints the unique Category names
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class MainClass {
public static void Main() {
List
from p in products
select p.Category)
.Distinct();
Console.WriteLine(“Category names:”);
foreach (var n in categoryNames) {
Console.WriteLine(n);
}
}
static List
List
empTree.Add(new Product { ProductName = “A”, Category = “O”, UnitPrice = 12, UnitsInStock = 5, Total = 36, OrderDate = new DateTime(2005, 1, 1), Id = 1 });
empTree.Add(new Product { ProductName = “B”, Category = “O”, UnitPrice = 2, UnitsInStock = 4, Total = 35, OrderDate = new DateTime(2005, 1, 1), Id = 1 });
empTree.Add(new Product { ProductName = “C”, Category = “O”, UnitPrice = 112, UnitsInStock = 3, Total = 34, OrderDate = new DateTime(2005, 1, 1), Id = 1 });
empTree.Add(new Product { ProductName = “D”, Category = “O”, UnitPrice = 112, UnitsInStock = 0, Total = 33, OrderDate = new DateTime(2005, 1, 1), Id = 1 });
empTree.Add(new Product { ProductName = “E”, Category = “O”, UnitPrice = 1112, UnitsInStock = 2, Total = 32, OrderDate = new DateTime(2005, 1, 1), Id = 1 });
empTree.Add(new Product { ProductName = “F”, Category = “O”, UnitPrice = 11112, UnitsInStock = 0, Total = 31, OrderDate = new DateTime(2005, 1, 1), Id = 1 });
return empTree;
}
}
class Product : IComparable
public string ProductName { get; set; }
public string Category { get; set; }
public int UnitPrice { get; set; }
public int UnitsInStock { get; set; }
public int Total { get; set; }
public DateTime OrderDate { get; set; }
public int Id { get; set; }
public override string ToString() {
return String.Format(“Id: {0}, Name: {1} , Category: {3}”, this.Id, this.ProductName, this.Category);
}
int IComparable
if (other == null)
return 1;
if (this.Id > other.Id)
return 1;
if (this.Id < other.Id) return -1; return 0; } } [/csharp]
Distinct Operator
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
public static void Main() {
string[] presidents = {"Grant", "Harding", "Harrison", "Hayes", "Hoover", "Jackson"};
Console.WriteLine("presidents count: " + presidents.Count());
IEnumerable<string> presidentsWithDupes = presidents.Concat(presidents);
Console.WriteLine("presidentsWithDupes count: " + presidentsWithDupes.Count());
IEnumerable<string> presidentsDistinct = presidentsWithDupes.Distinct();
Console.WriteLine("presidentsDistinct count: " + presidentsDistinct.Count());
}
}
Shows how queries can be reused
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class MainClass {
public static void Main() {
int[] numbers = new int[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNumbers =
from n in numbers
where n <= 3
select n;
Console.WriteLine("First run numbers <= 3:");
foreach (int n in lowNumbers) {
Console.WriteLine(n);
}
for (int i = 0; i < 10; i++) {
numbers[i] = -numbers[i];
}
Console.WriteLine("Second run numbers <= 3:");
foreach (int n in lowNumbers) {
Console.WriteLine(n);
}
}
}
[/csharp]
Deferred Query Execution
using System;
using System.Linq;
class MainClass {
static double Square(double n) {
return Math.Pow(n, 2);
}
public static void Main() {
int[] numbers = { 1, 2, 3 };
var query = from n in numbers select Square(n);
foreach (var n in query)
Console.WriteLine(n);
}
}
Immediate Query Execution
using System;
using System.Linq;
class MainClass {
static double Square(double n) {
Console.WriteLine("Computing Square(" + n + ")...");
return Math.Pow(n, 2);
}
public static void Main() {
int[] numbers = { 1, 2, 3 };
var query = from n in numbers select Square(n);
foreach (var n in query.ToList())
Console.WriteLine(n);
}
}

