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());
}
}
Software Development
software development notes, main category for Cs, Java, and other programming experiences.
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]
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);
}
}
}
Use Linq Distinct to get the distinct value in an array
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());
}
}
most query operators is that they execute not when constructed, but when enumerated
using System;
using System.Collections.Generic;
using System.Linq;
public class MainClass {
public static void Main() {
var numbers = new List<int>();
numbers.Add(1);
IEnumerable<int> query = numbers.Select(n => n * 10);
numbers.Add(2);
foreach (int n in query)
Console.Write(n + "|");
}
}
A deferred execution query is reevaluated when you reenumerate:
using System;
using System.Collections.Generic;
using System.Linq;
public class MainClass {
public static void Main() {
var numbers = new List<int>() { 1, 2 };
IEnumerable<int> query = numbers.Select(n => n * 10);
foreach (int n in query) Console.Write(n + "|");
numbers.Clear();
foreach (int n in query) Console.Write(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);
}
}