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); } }
Author: coder
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); } }
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 + "|"); } }
Use Aggregate Operators: Count
using System; using System.Collections.Generic; using System.Linq; using System.Text; public class MainClass { public static void Main() { int[] factorsOf300 = { 2, 2, 3, 5, 5 }; int uniqueFactors = factorsOf300.Distinct().Count(); Console.WriteLine("There are {0} unique factors of 300.", uniqueFactors); } }
Conditional Count
using System; using System.Collections.Generic; using System.Linq; using System.Text; public class MainClass { public static void Main() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; int oddNumbers = numbers.Count(n => n % 2 == 1); Console.WriteLine("There are {0} odd numbers in the list.", oddNumbers); } }
Nested Count
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class MainClass {
public static void Main() {
List
var orderCounts =
from c in customers
select new { c.CustomerId, OrderCount = c.Orders.Count() };
Console.Write(orderCounts);
}
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 });
List
l.Add(new Customer { CompanyName = “A”, Region = “R1”, UnitsInStock = 1, Orders = empTree, CustomerId = 0 });
l.Add(new Customer { CompanyName = “B”, Region = “R2”, UnitsInStock = 2, Orders = empTree, CustomerId = 1 });
l.Add(new Customer { CompanyName = “C”, Region = “R3”, UnitsInStock = 3, Orders = empTree, CustomerId = 2 });
l.Add(new Customer { CompanyName = “D”, Region = “R4”, UnitsInStock = 4, Orders = empTree, CustomerId = 3 });
l.Add(new Customer { CompanyName = “E”, Region = “R5”, UnitsInStock = 5, Orders = empTree, CustomerId = 4 });
return l;
}
}
class Customer : IComparable
public string CompanyName { get; set; }
public string Region { get; set; }
public List
public int UnitsInStock { get; set; }
public int CustomerId { get; set; }
public override string ToString() {
return String.Format(“Id: {0}, Name: {1}, Region: {3}”, this.CustomerId, this.CompanyName, this.Region);
}
int IComparable
if (other == null)
return 1;
if (this.CustomerId > other.CustomerId)
return 1;
if (this.CustomerId < other.CustomerId)
return -1;
return 0;
}
}
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]
Display Linq result in a DataGridView
using System.Drawing; using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; public class Book { public String Title { get; set; } public override String ToString() { return Title; } } public class FormBooks : Form { static public Book[] Books = { new Book {Title="F"}, new Book {Title="B"} }; public FormBooks() { InitializeComponent(); } private void FormStrings_Load(object sender, EventArgs e) { String[] books = { "F", "A", "B", "R", "B" }; var query = from book in books where book.Length > 10 orderby book select new { Book = book.ToUpper() }; dataGridView1.DataSource = query.ToList(); } private void InitializeComponent() { this.dataGridView1 = new System.Windows.Forms.DataGridView(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); this.SuspendLayout(); // // dataGridView1 // this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill; this.dataGridView1.Location = new System.Drawing.Point(10, 10); this.dataGridView1.Name = "dataGridView1"; this.dataGridView1.Size = new System.Drawing.Size(272, 251); this.dataGridView1.TabIndex = 0; // // FormStrings // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 271); this.Controls.Add(this.dataGridView1); this.Name = "FormStrings"; this.Padding = new System.Windows.Forms.Padding(10); this.Text = "FormStrings"; this.Load += new System.EventHandler(this.FormStrings_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); this.ResumeLayout(false); } private System.Windows.Forms.DataGridView dataGridView1; public static void Main() { Application.Run(new FormBooks()); } }