Use Contains, Length and ToUpper from Linq

image_pdfimage_print
   
 

using System;
using System.Collections.Generic;
using System.Linq;
public class MainClass {
    public static void Main() {
        string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
        IEnumerable<string> filtered = names.Where(n => n.Contains("a"));
        IEnumerable<string> sorted = filtered.OrderBy(n => n.Length);
        IEnumerable<string> finalQuery = sorted.Select(n => n.ToUpper());

        foreach (string name in filtered)
            Console.Write(name + "|");        

        Console.WriteLine();
        foreach (string name in sorted)
            Console.Write(name + "|");        

        Console.WriteLine();
        foreach (string name in finalQuery)
            Console.Write(name + "|");        
    }
}

    


Chaining Query Operators/extracts all strings containing the letter “a”, sorts them by length, and then converts the results to uppercase

image_pdfimage_print
   
 

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

class LinqDemo {
    static void Main() {
        string[] names = { "J", "P", "G", "P" };

        IEnumerable<string> query = names
          .Where(n => n.Contains("a"))
          .OrderBy(n => n.Length)
          .Select(n => n.ToUpper());

        foreach (string name in query) Console.Write(name + "|");
    }
}

    


First Select Prototype

image_pdfimage_print
   
 


using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
    public static void Main() {
        string[] presidents = {"A", "Ar", "Buc", "Bush", "Carte", "Clevel"};

        var nameObjs = presidents.Select(p => new { p, p.Length });

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

    


Select Prototype: string length

image_pdfimage_print
   
 

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
    public static void Main() {
        string[] presidents = {"A", "Ar", "Buc", "Bush", "Carte", "Clevel"};
        var nameObjs = presidents.Select(p => new { LastName = p, Length = p.Length });
        foreach (var item in nameObjs)
            Console.WriteLine("{0} is {1} characters long.", item.LastName, item.Length);

    }
}

    


Second Select Prototype

image_pdfimage_print
   
 

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
    public static void Main() {
        string[] presidents = {"A", "B", "Bu", "Bush", "C", "Cl"};

        var nameObjs = presidents.Select((p, i) => new { Index = i, LastName = p });

        foreach (var item in nameObjs)
            Console.WriteLine("{0}. {1}", item.Index + 1, item.LastName);

    }
}

    


Where by Prototype

image_pdfimage_print
   
 

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
    public static void Main() {
        string[] presidents = {"Ad", "Ar", "Bu", "Bu", "Ca", "Cl"};

        IEnumerable<string> sequence = presidents.Where((p, i) => (i &amp; 1) == 1);

        foreach (string s in sequence)
            Console.WriteLine("{0}", s);

    }
}