|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionA lot of good articles are doing a great job of explaining LINQ. From syntax to concept and projects, LINQ is well covered. The aim of this article is not to repeat / recycle this material into one big blob. Check out the list of good books for details. My favorite is this little pocket reference. The concept of this article is based on this short video. This article is a graph of how we reached up to LINQ, and the evolution of C# from 1.0 to 3.5. Is LINQ a new thing or just sweet syntax for old ugly code? In this article, we will look at the journey of C# up to LINQ in less than 10/15 minutes. So let's get started and demystify the LINQ. We will need some scaffolding code. We will use a class City
{
private string _name;
private string _state;
public City(string name, string state)
{
this._name = name;
this._state = state;
}
public string Name
{
get
{
return _name;
}
}
public string State
{
get
{
return _state;
}
}
}
Let’s create a collection of cities and then iterate over this collection using List<City> cities = new List<City>();
City c = new City("Santa Ana", "CA");
City c1 = new City("Irvine", "CA");
City c2 = new City("Bloomington", "IN");
cities.Add(c);
cities.Add(c1);
cities.Add(c2);
foreach (City tempCity in cities)
{
Console.WriteLine("City Name : {0} and State : {1}",tempCity.Name, tempCity.State);
}
Let’s filter this collection based on some criteria. Say we want cities from California only. A very simple solution will be to add an if (tempCity.State == "CA")
Console.WriteLine("City Name : {0} and State : {1}", tempCity.Name, tempCity.State);
This is a good enough solution. The problem with this code is “tight coupling”. C# 1.0 provided a delegate based composable solution to this problem. The following is the refactored version of this code: Step 1delegate bool IsParticularState(City c);
Step 2static bool IsCalifornia(City c)
{
return c.State == "CA" ? true : false;
}
Step 3PrintCityInfo(cities, new IsParticularState(IsCalifornia));
Step 4static void PrintCityInfo(List
As shown in step 1, we added a Notice the filter instead of the simple static bool IsCalifornia(City c)
Still this is too much of a code for implementing a simple filter logic. Wouldn't it be nice if we don't have to add an additional filter method. Yes, that's where C# 2.0 anonymous methods will be helpful. So, the code above can be refactored as follows: PrintCityInfo(cities,delegate(City ctemp){return (ctemp.State == "CA"?true:false);});
Notice the inline This is a good move, but still needs a PrintCityInfo(cities,ctemp=>ctemp.State=="CA");
Isn't this code elegant? From 7+ lines, a And now comes LINQ. What if we want to filter the end result on multiple conditions or want to sort the output based on some predefined order like PrintCityInfousingLINQ(from ctemp in cities
where ctemp.State=="CA"
select ctemp);
static void PrintCityInfousingLINQ(IEnumerable
Here we defined a new method Let's order the end result by PrintCityInfousingLINQ(from ctemp in cities
where ctemp.State=="CA"
orderby ctemp.Name
select ctemp);
Without the ConclusionFrom the trivial from ctemp in cities
where ctemp.State=="CA"
orderby ctemp.Name
select ctemp;
... using At a midnight debate by the empty parking lot of a grad school, someone said something to the effect of –“power of any programming language is its ability to say very complex things very easily. Like The woods are lovely, dark, and deep, But I have promises to keep…” LINQ is one more step in the right direction. What do you think? History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||