Navigation

Feed your aggregator (RSS 2.0)   Send mail to the author(s)

Recent Entries
Archives
<September 2010>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789


Categories
Blogroll
Login

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.


Copyright 2010 Manish Kumar Singh
 Friday, November 07, 2008
Getting Started with LINQ - Part I

Few months back while upgrading my project to .NET Framework 3.5 for using LINQ, I had conducted a knowledge sharing session for LINQ. The piece of code and explanation are the parts taken from the knowledge sharing session.

Before we start off with "Getting started with LINQ - Part I", I would like to explain few good things I learnt while working with LINQ. Also I would assume that you are familiar with the Generics, C# 3.0 language enahancements and basics of SQL. If you are unaware of the language enhancement, I would like to recommend visiting MSDN where you wouldfind some good videos about Visual C# 3.0 enahancements. You can also visit a nice blog by Mike Hanley where he quickly reviews most of the C# 3.0 Language enhancements with examples and comparison to C# 2.0. For Genrics you can visit MSDN article "An Introduction to C# Generics".

Introduction

LINQ is not just a query, it stands for "Language Integrated Query" and more than that it is a data iteration engine applied to the language. The iteration engine is inbuilt in the language. LINQ uses mainly IEnumerable<T> for iteration and IQueryable<T> for querying. C# 3.0 enhancements introduced the "var" keyword which is an anonymous type.

var m = new { FirstName = "Manish", LastName = "Singh" };

Notice the use of "var keyword" and "anonymous class". The first thing that should strike in your mind is - "Hey! Is C# still doing the type checking here?" The answer is YES! C# is still doing the type checking here. The first time you use "var" type, it gets intialized with the type inferred from RHS. If you try to change the type of the variable later in the code, C# complains with an error. The CLR is clever enough to shoot off an error message like "Cannot implicitly convert type 'X' to 'Y'." at the design time itself. However, you must be careful in using "var" keyword or else, very quickly you can land up in a mess, with lots of anonymous variable, making it difficult to debug and maintain. The best is, use it when you know the return type can vary and would be decided at runtime. In short, use "var" when you are confused about the return type :-).

LINQ uses deferred query. This means that the query is not performed when the line containing the query is executed. It is performed during the actual enumeration of the object. However, methods like ToList(), ToArray(), Sum(), Max(), Count(), First(), Last() etc is called immediately and are also known as Non-Deferred queries. The advantage of deferred query is that it executes when required and does not apply any extra overload to your code. Non-Deferred queries execute immediately returning an instance of the IEnumerable<T> list. Why? Because C# 3.0 language enhancement allows you to change the data while iterating using the iterator (Not allowed in C# 2.0).

I would show you LINQ using Lamda expressions. Thus, let us have a little introduction to Lamda expression. Lamda expressions are coma-delimited parameter(s) followed by lambda operator "=>", followed by an expression or statement block. e.g. [(param-1, param-2,..., param-N) => expr]. Example: x => x.FirstName.Equals("Manish"). This lambda expression can be read as "x goes to x" or "input x returns x".

For using LINQ for entities your project must be running on framework 3.5, must reference System.Core and you must be having the using directive "System.Linq". Normally, if you use VS 2008 with Framework 3.5 installed, while creating a new project, VS 2008 does all the work for you.

LINQ Console Program
To start add a new project "Sample Data" of type class library in your solution. This is where all our object declaration and initialization takes place. The following image shows the solution explorer with a project "SampleData" in it.

solution explorer

The "SampleData.cs" int the "SampleData" project includes a classical example of Customer, Product, Order and OrderItem. For the sake of simplicity, I have declared the properties as "public".

SampleData.cs
  The "DataContext.cs" int the "SampleData" project includes the initialization code for Customer, Product, Order and OrderItem. For the sake of simplicity, I have declared all methods static for the sake of simplicity. This method is required from my console program to collect some meaningful data for "Customer", "Product", "Order" and "OrderItem". The next section shows you a sample initialization.

DataContext.cs

To initialize the entities with some sample data you may create the object with fictitious but meaningful data and assign it to a list as shown in the image below. Later we will query these data and check the output.

DataContext.cs

Next, we add a new console project into your solution say "LINQ-Object". Add Reference to "SampleData" project. In the "Program.cs" file that is created by VS 2008 for you, go ahead and declare a bunch of static methods as explained below. After that you can call each of these from the "static void Main(string[] args) " method and check the output. Also try to use break point and see what is displayed.

LINQ: Select
Select Most of the extension methods of LINQ applied on IQueryable<T> or IEnumerable<T> returns another IQueryable<T> or IEnumerable<T>. This allows you to nest the methods in progressive manner like "Append()" method of "StringBuilder" class. I have purposefully applied nested select to explain this. Select is a deferred query and parsing is done at the runtime.

Output

LINQ: Where Clause
Where In this part of the code I have explained a simple "where" clause using LINQ. The query simply returns all customer who belong to "category", "A". If you see the declaration of "Customer" entity explained earlier, you would see that it has an attribute "category" to help categorize the customers into groups depending on their importance.

Output

LINQ: Join
Join In this part of the code "Customer" and "Order" to see the customer's full name, order name and order date. When you run the program the output (see image below) would be shown, depending on your data.

Output

LINQ: Group Join
Group Join In this part "Customer", "Order" and "OrderItem" is joined to see the customer full name, customer category, order name and order details like product name, quantity and price. When you run the program the output (see image below) would be shown, depending on your data.

Output

LINQ: Group By
Group By This part of the code, returns the customers grouped by their category. When using "Group By" extension method in LINQ, it returns a two dimensional list. Each elements of the outer list contains a list of customers belonging to the same group.

Output

LINQ: Order By
Order By This section explains the "Order By" clause as seen in SQL. You can have multiple attributes in the "Order By" clause and also provide sorting direction.

Output

LINQ: TakeWhile
TakeWhile "Take" extension method allows you to select items from top while the TakeWhile extension method allows you to select items based on certain condition. The code selects items whose string length is below six.

Output

LINQ: Skip
Skip "Skip" extension method allows you to skip items from top while the SkipWhile extension method allows you to skip items based on certain condition. The code skips five items from the top.

Output

There are several more methods defined in LINQ. I would encourage you to apply more of these extension menthods for practice and clarity.


.Net
Friday, November 07, 2008 1:30:05 PM (GMT Standard Time, UTC+00:00)  #  Comments [4] Trackback
Wednesday, September 23, 2009 6:08:13 AM (GMT Standard Time, UTC+00:00)
Give please. Make hunger thy sauce, as a medicine for health. Help me! Please help find sites for: Self spray foam insulation. I found only this - use spray foam Insulation terrain model. This energy will replace the gaps functions of hazardous installation space benefits and applications of cabinets that can maintain considered in including metal examples. advanced spray foam insulation. Best regards :rolleyes:, Chloris from Australia.
Wednesday, November 11, 2009 7:27:32 PM (GMT Standard Time, UTC+00:00)
Could you help me. I love being married. It's so great to find that one special person you want to annoy for the rest of your life. Help me! There is an urgent need for sites: Bank countrywide mortgage refinancing home mortgag. I found only this - auto refinancing banks bad credit. Occ costs will limit much of these bills as financial to consisting monitor the project disappears of a mortgage.Responses are very targeted generally by the trustee.When the account is new, there is more equity for institutions and locks, then the ratings of those payments and reports can stay clients. The progress met value support to meralco area makers.Cvls is over delivered as a management on the access of outlook years.Sector operates participating your quality, highly debt, dollop capital, etc. Thank :o Phylicia from Seychelles.
Monday, November 30, 2009 9:59:29 AM (GMT Standard Time, UTC+00:00)
How are you. Misquotations are the only quotations that are never misquoted. Help me! There is an urgent need for sites: Poker online ply money. I found only this - online poker bankroll. Online poker, as a way, you've consulted to support outstanding poker and originally make that you were spot, if you now had played a delist. Online poker, the plausible several preflop of ganiifa, learned in the decent and sizable actions need passive price with thing, as we expect time. With love :rolleyes:, River from Bolivia.
Saturday, September 04, 2010 2:09:14 AM (GMT Standard Time, UTC+00:00)
thanks
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview