LINQ – Uniformity All Over

I have spent a while with LINQ and still feel very new whenever I explore some new power. Here I am going to describe you how LINQ uniformly allows you write for the various types of data.

In-memory data source

++++++++++++++++

List<int> arrInt = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

IEnumerable<int> q1 = from i in arrInt

                    where i % 2 == 0

                    select i;

Operating System data source (bringing into in-memory)

+++++++++++++++++++++++++++++++++++++

var q2 = from p in System.Diagnostics.Process.GetProcesses()

        where p.Threads.Count > 9

        orderby p.ProcessName

        select new

        {

            PName = p.ProcessName,

            Count = p.Threads.Count

        };

Reflection data source

+++++++++++++++

var q3 = from m in typeof(string).GetMethods()

        where !m.IsStatic

        orderby m.Name

        group m by m.Name into g

        where g.Count() > 2

        select new { Name = g.Key, Count = g.Count() };

XML data source

+++++++++++

var data = XElement.Load(@"C:\Custs.xml").Descendants("cust");

var q4 = from c in data

        where (string)c.Attribute("Country") == "USA"

        select c.Attribute("CompanyName").Value;

LINQ to SQL for Relational data source

++++++++++++++++++++++++++

NWDataContext db = new NWDataContext();

db.Log = Console.Out;

var q = from c in db.Customers

        where c.City == "London"

        select c.CompanyName;

foreach (var k in q)

{

    Console.WriteLine(k);

}

Namoskar!!!