use Concat with array elements

   
 
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, 2, 3, 4, 5, 6, 7, 8, 9 };
       int[] moreNumbers = { 10, 11, 12, 13 };

       var query = numbers.Concat(moreNumbers);
   }
}

    


Multiple from: filtering on value list can be done before selecting their orders

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class MainClass {
public static void Main() {
List customers = GetCustomerList();

DateTime cutoffDate = new DateTime(2005, 1, 1);

var orders =
from c in customers
where c.Region == “R1”
from o in c.Orders
where o.OrderDate >= cutoffDate
select new { c.CustomerId, o.Id };

}
static List GetCustomerList() {
List empTree = new 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 = new 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 Orders { get; set; }
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.CompareTo(Customer other) {
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.CompareTo(Product other) {
if (other == null)
return 1;
if (this.Id > other.Id)
return 1;

if (this.Id < other.Id) return -1; return 0; } } [/csharp]

from Assignment: select all orders where the order total is greater than 2000.00

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class MainClass {
public static void Main() {
List customers = GetCustomerList();

var orders =
from c in customers
from o in c.Orders
where o.Total >= 35.0M
select new { c.CustomerId, o.Id, o.Total };

foreach (var p in orders) {
Console.WriteLine(p.CustomerId);
}
}
static List GetCustomerList() {
List empTree = new 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 = new 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 Orders { get; set; }
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.CompareTo(Customer other) {
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.CompareTo(Product other) {
if (other == null)
return 1;
if (this.Id > other.Id)
return 1;

if (this.Id < other.Id) return -1; return 0; } } [/csharp]

uses a compound from clause to select all orders where the order was made in 1998 or later.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class MainClass {
public static void Main() {
List customers = GetCustomerList();

var orders =
from c in customers
from o in c.Orders
where o.OrderDate >= new DateTime(1998, 1, 1)
select new { c.CustomerId, o.Id, o.OrderDate };
foreach (var p in orders) {
Console.WriteLine(p.CustomerId);
}
}
static List GetCustomerList() {
List empTree = new 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 = new 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 Orders { get; set; }
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.CompareTo(Customer other) {
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.CompareTo(Product other) {
if (other == null)
return 1;
if (this.Id > other.Id)
return 1;

if (this.Id < other.Id) return -1; return 0; } } [/csharp]

Compound from: prints all orders whose total is less that 500

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class MainClass {
public static void Main() {

List customers = GetCustomerList();

var orders =
from c in customers
from o in c.Orders
where o.Total < 500.00M select new { c.CustomerId, o.Id, o.Total }; foreach(var p in orders){ Console.WriteLine(p.CustomerId); } } static List GetCustomerList() {
List empTree = new 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 = new 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 Orders { get; set; }
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.CompareTo(Customer other) {
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.CompareTo(Product other) {
if (other == null)
return 1;
if (this.Id > other.Id)
return 1;

if (this.Id < other.Id) return -1; return 0; } } [/csharp]

Compound from: identifies ordered pairs of integers

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class MainClass {
public static void Main() {

int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
int[] numbersB = { 1, 3, 5, 7, 8 };

var pairs =
from a in numbersA
from b in numbersB
where a < b select new { a, b }; Console.WriteLine("Pairs where a < b:"); foreach (var pair in pairs) { Console.WriteLine("{0} is less than {1}", pair.a, pair.b); } } } [/csharp]

The standard object creation and initialization vs Linq

   
 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
class Person {
    int _id;
    int _idRole;
    string _lastName;
    string _firstName;

    public int ID {
        get { return _id; }
        set { _id = value; }
    }

    public int IDRole {
        get { return _idRole; }
        set { _idRole = value; }
    }

    public string LastName {
        get { return _lastName; }
        set { _lastName = value; }
    }

    public string FirstName {
        get { return _firstName; }
        set { _firstName = value; }
    }
}
class Program {
    static void Main(string[] args) {
        Person p1 = new Person();
        p1.FirstName = "B";
        p1.LastName = "A";

        Person p2 = new Person { FirstName = "Tom", LastName = "Gray" };
    }

}