Filter string by its length

   
 

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

class LinqDemo {
    static void Main() {
        string[] names = { "Tom", "Dick", "Harry" };


        IEnumerable<string> filteredNames = names.Where(n => n.Length >= 4);
        foreach (string name in filteredNames) Console.Write(name + "|");
    }
}

    


Assign the loop variable to another variable declared inside the statement block

   
 

using System;
using System.Collections.Generic;
using System.Linq;
public class MainClass {
    public static void Main() {
        IEnumerable<char> vowels = "aeiou";
        IEnumerator<char> rator = vowels.GetEnumerator();
        IEnumerable<char> query = "Not what you might expect";


        foreach (char vowel in "aeiou") {

            char temp = vowel;
            query = query.Where(c => c != temp);
        }
    }
}

    


use where clause in a while loop

   
 

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

        IEnumerable<char> vowels = "aeiou";
        IEnumerator<char> rator = vowels.GetEnumerator();
        IEnumerable<char> query = "Not what you might expect";

        char vowel;
        while (rator.MoveNext()) {
            vowel = rator.Current;
            query = query.Where(c => c != vowel);
        }
    }
}

    


Two where clauses

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

class Employee {
    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 Salary {
    int _id;
    int _year;
    double _salary;

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

    public int Year {
        get { return _year; }
        set { _year = value; }
    }

    public double SalaryPaid {
        get { return _salary; }
        set { _salary = value; }
    }
}
public class MainClass {
    public static void Main() {
        List<Employee> people = new List<Employee> {
              new Employee  { ID = 1, IDRole = 1, LastName = "A", FirstName = "B"},
              new Employee  { ID = 2, IDRole = 2, LastName = "G", FirstName = "T"}
            };
        List<Salary> salaries = new List<Salary> {
               new Salary { ID = 1, Year = 2004, SalaryPaid = 10000.00 },
               new Salary { ID = 1, Year = 2005, SalaryPaid = 15000.00 },
               new Salary { ID = 1, Year = 2005, SalaryPaid = 15000.00 }
            };
        IEnumerable<Salary> q = from p in people
                                where p.ID == 1
                                from s in salaries
                                where s.ID == p.ID
                                select s;


    }
}

    


Filtered: prints the name of each element of an integer array that is less than 5

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};
string[] digits = { “zero”, “one”, “two”, “three”};

var lowNums =
from n in numbers
where n < 5 select digits[n]; Console.WriteLine("Numbers < 5:"); foreach (var num in lowNums) { Console.WriteLine(num); } } } [/csharp]

Use && in where clause

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

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

var expensiveInStockProducts =
from p in products
where p.UnitsInStock > 0 && p.UnitPrice > 3.00M
select p;

foreach (var product in expensiveInStockProducts) {
Console.WriteLine(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]