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());
}
}
DefaultIfEmpty Demo
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
public static void Main() {
string[] presidents = {"G", "H", "a", "H", "over", "Jack"};
string name =
presidents.Where(n => n.Equals("H")).DefaultIfEmpty("Missing").First(); Console.WriteLine(name);
}
}
Without Using DefaultIfEmpty
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
public static void Main() {
string[] presidents = {"G", "H", "a", "H", "over", "Jack"};
string jones = presidents.Where(n => n.Equals("H")).First();
if (jones != null)
Console.WriteLine("H was found");
else
Console.WriteLine("H was not found");
}
}
Using DefaultIfEmpty in Where clause
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
public static void Main() {
string[] presidents = {"G", "H", "a", "H", "over", "Jack"};
string jones = presidents.Where(n => n.Equals("H")).DefaultIfEmpty().First();
if (jones != null)
Console.WriteLine("H was found.");
else
Console.WriteLine("H was not found.");
}
}
Deferred Query Execution: shows how query execution is deferred until the query is enumerated at a foreach statement.
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 };
int i = 0;
var q =
from n in numbers
select ++i;
foreach (var v in q) {
Console.WriteLine("v = {0}, i = {1}", v, i);
}
}
}
How queries can be executed immediately with operators such as ToList().
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 };
int i = 0;
var q = (
from n in numbers
select ++i)
.ToList();
// The local variable i has already been fully
// incremented before we iterate the results:
foreach (var v in q) {
Console.WriteLine("v = {0}, i = {1}", v, i);
}
}
}
The aggregation operators return a scalar value
using System;
using System.Collections.Generic;
using System.Linq;
public class MainClass {
public static void Main() {
int[] numbers = { 10, 9, 8, 7, 6 };
int count = numbers.Count();
int min = numbers.Min();
}
}

