July 29th, 2008
OfType operator can be used to return objects of a certain type. For example if we have an array of objects which contains both strings and numbers, we can use OfType operator to extract only strings or numbers. In this example I have an array of objects which is initialised like this:
object[] goop = { “string1″, 2, 5, “string2″, “string3″ };
If I want to extract just string items out of this array I can use a query like this:
var onlyStrings =
from g in goop.OfType<string>()
select g;
This will give me only string items from my goop array
foreach (var item in onlyStrings)
{
Console.WriteLine(item);
}
I can modify the query to extract only integers which will look like this:
var onlyStrings =
from g in goop.OfType<int>()
select g;
While it is not a good practice to have an array which contains strings and integers, we could be working with some legacy code. OfType operator will come in handy in such situations.
1 Comment |
LINQ |
Permalink
July 3rd, 2008
Joseph Albahari who co-authored C# 3.0 in a Nutshell has written a fantastic tool for writing LINQ queries called LINQPad. Lately I have been working extensively with LINQ and I often feel the need to write some code quickly to test my query. I usually end up writing a simple console application or a test. In such instances where you want to quickly verify the results of your query, LINQPad really shines. The tool itself is very simple and does not require an installer. Just download the exe and you are good to go.
This is what it looks like:
Technorati Tags: LINQ, LINQPad
No Comments » |
LINQ |
Permalink
June 23rd, 2008
LINQ comes packed with few useful ordering operators. These are: OrderBy, ThenBy, OrderByDescending and ThenByDescending. I will walkthrough these operators using an example of cities. Here is the code for City class.
public class City
{
public string Name { get; set; }
public string Country { get; set; }
}
Below is the intitializer for cities collection.
List<City> cities =
new List<City>
{
new City{ Name = "Sydney", Country = "Australia" },
new City{ Name = "New York", Country = "USA" },
new City{ Name = "Paris", Country = "France" },
new City{ Name = "Milan", Country = "Spain" },
new City{ Name = "Melbourne", Country = "Australia" },
new City{ Name = "Auckland", Country = "New Zealand" },
new City{ Name = "Tokyo", Country = "Japan" },
new City{ Name = "New Delhi", Country = "India" },
new City{ Name = "Hobart", Country = "Australia" }
};
I will now walk you through ordering operators. First one we will look at is the OrderBy operator. OrderBy as the name implies orders the collection with a specified field in ascending order. To order our collection by country we can use the following code.
var result =
from c in cities
orderby c.Country
select c;
Using the loop below I can see that my collection has been ordered by country.
foreach (var item in result)
{
Console.WriteLine(
string.Format("Name: {0}\t Country: {1}", item.Name, item.Country));
}
And here is the output.
ThenBy operator can be used to order an already ordered collection using another criteria. For example we can use it to order our list by Country and then by Name.
var result =
cities
.OrderBy(c => c.Country)
.ThenBy(c => c.Name);
I can also use ThenBy explicitly in my OrderBy like this.
var result =
from c in cities
orderby c.Country, c.Name
select c;
OrderByDescending orders the collection in descending manner. This can be accomplishes by simply using the descending keyword.
var result =
from c in cities
orderby c.Country descending
select c;
ThenByDescending orders an already ordered collection using another criteria but this time in descending manner.
Explicit use of ThenByDescending:
var result =
cities
.OrderBy(c => c.Country)
.ThenByDescending(c => c.Name);
Implicit use of ThenByDescending:
var result =
from c in cities
orderby c.Country, c.Name descending
select c;
By using ThenByDescending operator explicitly or implicitly we get the same output.
Ordering is just one category of standard LINQ query operators. The more I work with LINQ, the more I find out how interesting and useful LINQ is. Stay tuned for more posts on LINQ.
Technorati Tags: LINQ

1 Comment |
LINQ |
Permalink
June 21st, 2008
“Where” operator is the most commonly used LINQ operator. For those familiar with SQL “Where” operator will be obvious. It filters the results based on specified criteria. Below is an example using a list of cities.
List<string> cities =
new List<string> {
“New York”,
“Sydney”,
“Paris”,
“New Delhi” };
var result =
from c in cities
where c.StartsWith(“New”)
select c;
foreach (string item in result)
{
Console.WriteLine(item);
}
Here we used the where operator to select cities which start with “New”. Below is the output:
Technorati Tags: LINQ
No Comments » |
LINQ |
Permalink