Practice Linq

 

This is my first learning & practice post and the technology I picked is Linq (Language Integreated Query).  First I downloaded the LINQPad from http://www.linqpad.net/; and get Northwind database from http://northwinddatabase.codeplex.com/. I tried to learn LINQPad, which is a nice tool to run not only LINQ, but also C# statements and expressions.

Then I downloaded 101 LINQ Samples from http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b for revision.

Revision Notes

  1. Several clauses in method syntax provides index value to use, such as Where, Select, TakeWhile and SkipWhile.  For example,
    (new [] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }).Where((n, index) => n > index).Select(n => n)
  2. SelectMany method: Projects each element of a sequence to an IEnumerable<T> and flattens the resulting sequences into one sequence. For example,
    new [] {"donut", "muffin"}.SelectMany(element => element.ToCharArray())
  3. Reverse method: reversed from the order in the original source.
  4. Group by clause and for multiple columns grouping:
    new [] {1,2,3,4,5,6,7,8,9,10}.GroupBy(n=>n % 3).Select(g => g)
    from n in new [] {1,2,3,4,5,6,7,8,9,10}
    group n by n%3 into g
    select new { g.Key, g }
    group x by new { x.Column1, x.Column2 }
  5. Filter by object type:
    (new object[] { null, 1.0, "two", 3, "four", 5, "six", 7.0 }).OfType<double>()
  6. “Any” method is good to use for checking non-empty result instead of comparing count > 0; as “Any” method is just called next method from enumerable; therefore we don’t need to loop through the whole list.
  7. Be aware of query reuse, there is a good example in LINQ 101 Examples; which shows that you can run the same query with a simple filter before and after values changes.  And the result will return differently.

Q & A

  1. LINQ method and query expression, which should I use? which one is better?
    Quote from StackOverflow,

    Neither is better: they serve different needs. Query syntax comes into its own when you want to leverage multiple range variables. This happens in three situations: When using the let keyword, When you have multiple generators (from clauses), and When doing joins.

  2. How to debug Linq in Visaul Studio?
    Please refer to LINQ debugging in MSDN, and I found that you can set breakpoint in the predicate in VS2013 by moving the cursor to the predicate and hit F9.

Again, more hands-on practice is required to familiar with the LINQ syntax.  Work hard!

Sharing

Below are some queries I wrote for practice, you can copy and paste to LINQPad and run against Northwind database.

(from c in Customers where c.CustomerID.StartsWith("S") orderby c.ContactName select c).Skip(2).Take(3).Dump();

Customers.GroupBy(c => new { c.Country, c.PostalCode }).Select(g => g).Dump();

(from c in Customers group c by new { c.Country, c.PostalCode } into g select g).Dump();

(from c in Customers group c by new { c.Country, c.PostalCode } into g select new { g.Key, Total=g.Sum(c => c.Orders.Sum(o => o.OrderDetails.Sum(od => od.UnitPrice)))}).Dump();

(from c in Customers where c.CustomerID == "CACTU" select new { c.Country, c.PostalCode, s =c.Orders.Sum(o => o.OrderDetails.Sum(od => od.UnitPrice * od.Quantity))}).Dump();

(from c in Customers
join o in Orders on c.CustomerID equals o.CustomerID
join od in OrderDetails on o.OrderID equals od.OrderID
//where c.CustomerID == "CACTU"
group new {c, od } by new { c.Country, c.PostalCode } into g
select new {g.Key, s=g.Sum(r => r.od.UnitPrice * r.od.Quantity)}).Dump();
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s