Use where to filer object list

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

public class MainClass {
public static void Main() {
List products = GetProductList();

var soldOutProducts =
from p in products
where p.UnitsInStock == 0
select p;

Console.WriteLine(“Sold out products:”);
foreach (var product in soldOutProducts) {
Console.WriteLine(“{0} is sold out!”, product.ProductName);
}
}

static List GetProductList() {
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 });
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.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]

where clause

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

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) {
        List<Person> people = new List<Person> {
              new Person  { ID = 1, IDRole = 1, LastName = "A", FirstName = "B"},
              new Person  { ID = 2, IDRole = 2, LastName = "G", FirstName = "T"},
              new Person  { ID = 3, IDRole = 2, LastName = "G", FirstName = "M"},
              new Person  { ID = 4, IDRole = 3, LastName = "C", FirstName = "G"}
            };

        var query = people.Where((p, index) => p.IDRole == index);
    }
}

    


Use string method in where clause

   
 


using System;
using System.Linq;

public class MainClass {
    public static void Main() {
        string[] greetings = { "hello world", "hello LINQ", "hello" };

        var items =
         from s in greetings
         where s.EndsWith("LINQ")
         select s;

        foreach (var item in items)
            Console.WriteLine(item);
    }
}

    


Query with an Exception

   
 


using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
    public static void Main() {
        string[] presidents = {"AAAA", "aaaa", "bacDert", "B1234", "Carter"};

        IEnumerable<string> items = presidents.Where(s => Char.IsLower(s[4]));

        Console.WriteLine("After the query.");

        foreach (string item in items)
            Console.WriteLine(item);

    }
}

    


Query string value by String.StartsWith

   
 
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
    public static void Main() {

        string[] presidents = {"Ad", "Ar", "Bu", "B", "C", "C"};

        string president = presidents.Where(p => p.StartsWith("Ad")).First();

        Console.WriteLine(president);
    }
}

    


Query Using the Query Expression Syntax

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
public static void Main() {

string[] names = { “A123123”, “B123”, “C123123”, “E123”, “W” };
IEnumerable sequence = from n in names
where n.Length < 6 select n; foreach (string name in sequence) { Console.WriteLine("{0}", name); } } } [/csharp]