How to more then solve SilverLight DataGrid issue with anonymous types
If you worked with SilverLight’s DataGrid and LINQ you likely noticed that you cannot select anonymous types in your queries. The application simply freezes. That sucks!
You can find some short term workarounds using LINQ to XML.
However, there is another approach. Microsoft has a small library called DynamicLINQ, which you can read about at http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
You can download the solution and add Dynamic.cs to your SL project. It will not compile right away complaining about ReaderWriterLock not being available in SilverLight. I just commented those lines out for now. Here is a code snippet that creates a query on a generic list and returns a collection of dynamic data classes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq.Dynamic;
namespace SilverlightApplication1
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
List<Data> list = new List<Data>() { new Data() { Age = 5, Name = “Vasya”}};
var query = list.AsQueryable().Select(”new(Name as FirstName, Age, \”Male\” as Sex)”);
grid.ItemsSource = query;
}
}
public class Data
{
public string Name { get; set; }
public int Age { get; set; }
}
}
So, with this, in addition to what anonymous types let you do, you can also make your projections dynamic.

