Monday, November 8, 2010

LINQ Cast & LINQ over Dataset: Part 2

1. When to use OfType & Cast
2. What support C# provides to query over datasets and data tables (LINQ to dataset).
3. Implications of anonymous types.

OfType & Cast


public static void CastOperator()
{
ArrayList list = new ArrayList();
list.Add(1);
list.Add(2);
list.Add("ASP.NET 3.5 Unleashed");


IEnumerable numbers = list.Cast();
foreach (int number in numbers)
{
Console.WriteLine(number);
}
}


When you run the cast operator, you get an exception because the cast operator tries to cast every element in the collection to the specified type. If it does not cast, it will throw an exception. In contrast, the OfType operator will return objects that can be cast to the right type and skips the rest from the collection.

public static void OfTypeOperator()
{
ArrayList list = new ArrayList();
list.Add(1);
list.Add(2);
list.Add("ASP.NET 3.5 Unleashed");


IEnumerable numbers = list.OfType();
foreach (int number in numbers)
{
Console.WriteLine(number);
}
}

LINQ Over Dataset

Out-of-the-box, datasets, datatables and dataviews do not have the necessary infrastructure to work in a LINQ query. This is where Microsoft introduced the new extension called System.Data.DataSetExtensions.dll, which includes DataTable extensions, DataRow extensions and TypedTableBasedExtesions. To make a data table LINQ aware, you simply need to call the AsEnumerable extension, which will return an EnumerableRowCollection consisting of DataRows. Another very useful extension method is DataRowExtensions.Field(), which allows you to do safe casting in a LINQ query and helps prevent runtime exceptions. Once you apply the filter on a datatable using the LINQ query, the result is simply EnumerableRowCollection. In order to return back a table, we can use the new copytodatatable extension method on the result.


public static void DataSetExample(DataTable books)
{
var implicittype = from book in books.AsEnumerable()
where book.Field("Author") == "Stephen Walther"
select book;
var datatable = implicittype.CopyToDataTable();
}


Anonymous Types
The C# 3.0 language includes the ability to dynamically create new unnamed classes. This type of class is known as an anonymous type. An anonymous type has no name and is generated by the compiler based on the initialization of the object being instantiated. Since the class has no type name, any variable assigned to an object of an anonymous type must have some way to declare it. This is the purpose of the new C# 3.0 var keyword. Anonymous types allow you to define class on-the-fly using object initialization syntax and assigning to var keyword. At compile time, the C# compiler will generate a unique class which is not accessible from your code. All anonymous classes derive from System.Object and therefore inherit all the properties and methods of the object class. Anonymous classes are provided with overridden versions of Equals(), GetHashCode and ToString(). The ToString implementation for an anonymous book class would look something like this:


public class Book
{
public string Title { get; set; }
public string Author { get; set; }
}




public override string ToString()
{
StringBuilder builder = new StringBuilder();
builder.Append("{ BookTitle = ");
builder.Append(this.i__Field);
builder.Append(", BookAuthor = ");
builder.Append(this.i__Field);
builder.Append(" }");
return builder.ToString();
}

No comments: