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 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);

    }
}

    


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);
    }
}

    


To remove all vowels from a string.

   
 

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

        IEnumerable<char> query = "Not what you might expect";

        query = query.Where(c => c != &#039;a&#039;);
        query = query.Where(c => c != &#039;e&#039;);
        query = query.Where(c => c != &#039;i&#039;);
        query = query.Where(c => c != &#039;o&#039;);
        query = query.Where(c => c != &#039;u&#039;);

        foreach (char c in query) Console.Write(c); 
    }
}

    


A Query Using the Standard Dot Notation Syntax

   
 


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

        string[] names = { "A1", "B123", "C123123", "E", "W" };

        IEnumerable<string> sequence = names
         .Where(n => n.Length < 6)
         .Select(n => n);

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

    


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]