Monday, January 14, 2008

.NET 3.5 Linq w/Data Tables

Well I'm really starting to like LINQ. Man does it make it really easy to filter information very quickly. One question I had was how to query a DataTable. I tend to stay away from DataSet's because of the web (Async/Sync). Well I found the answer, you have to give the type similar to the syntax for generics. For example, here is a quicky:

var query = from c in table.AsEnumerable() WHERE c.Field("Week") = 25 select new { Week = c.Field("Week"), Person = c.Field("Person"), Guess = c.Field("Guess"), Actual = c.Field("Actual") }

Pretty simple syntax allows for some pretty powerful custom filtering in your DataTable. Take the results and bind them to a Grid and your done like Turkey.

-Costoda

No comments: