Scott Hanselman

The Weekly Source Code 48 - DynamicQueryable makes custom LINQ expressions easier

January 27, '10 Comments [23] Posted in ASP.NET | ASP.NET Dynamic Data | Open Source | Programming | Source Code
Sponsored By

NOTE: An alternative title to this post might be: "The Weekly Source Code 48: Making The Weekly Source Code 47 Suck Incrementally Less."

NOTE: This isn't a language feature! This works on both C# and VB!

Last week I wrote a post about Dynamic Linq Query Generation in order to solve a kind of meta-programming problem. I had a site that used ASP.NET Dynamic Data and I wanted to do a LINQ query against some data. However, because I was creating a template that didn't know enough at compile time to write a proper LINQ query that could, well, compile, I needed to creating my LINQ dynamically.

Be sure to hang in here with me, the awesome happens at the end.

I was trying to generate effectively this, at runtime

Items.Select(row => row.Property).Distinct.OrderBy(colvalue => colvalue)

And I succeeded with Tatham Oddie's help in doing it this sub-optimal way:

protected void Page_Init(object sender, EventArgs e) {
var items = Column.Table.GetQuery();
var entityParam = Expression.Parameter(Column.Table.EntityType, "row");

// row => row.Property
var columnLambda = Expression.Lambda(Expression.Property(entityParam, Column.EntityTypeProperty), entityParam);

// Items.Select(row => row.Property)
var selectCall = Expression.Call(typeof(Queryable), "Select", new Type[] { items.ElementType, columnLambda.Body.Type }, items.Expression, columnLambda);

// Items.Select(row => row.Property).Distinct
var distinctCall = Expression.Call(typeof(Queryable), "Distinct", new Type[] { Column.EntityTypeProperty.PropertyType }, selectCall);


// colvalue => colvalue
var sortParam = Expression.Parameter(Column.EntityTypeProperty.PropertyType, "sortValue");
var columnResultLambda = Expression.Lambda(sortParam, sortParam);

// Items.Select(row => row.Property).Distinct.OrderBy(colvalue => colvalue)
var ordercall = Expression.Call(typeof(Queryable), "OrderBy",
new Type[] { Column.EntityTypeProperty.PropertyType, columnResultLambda.Body.Type },
distinctCall, columnResultLambda);

var result = items.Provider.CreateQuery(ordercall);

foreach (var item in result) {
if (item != null) DropDownList1.Items.Add(item.ToString());
}
}

"Sub-optimal" is a programmer euphemism for crappy, hard to read, code that works. But what price my immortal soul?

Fortunately, Marcin from the ASP.NET team decided to come out of his apparent blogging vow of silence (lasting 18 months, no less) to save me.

Marcin points out that there's a sample from 2006 released under the Ms-PL (how is anyone supposed to know this?) called DynamicQueryable. You actually have this on your hard drive NOW. It's under  Samples\1033\CSharpSamples.zip\LinqSamples\DynamicQuery\DynamicQuery in your VS install directory.

In fact, His Gu-ness blogged about this in January of 2008 giving this VB example:

Dim Northwind As new NorthwindDataContext
Dim query = From p In Northwind.Products
Where p.CategoryID = 2 And UnitPrice > 3
Order By p.SupplierID
Select p
GridView1.DataSource = query
GridView1.DataBind()

But using the DynamicQuery library you can express the same thing like this, allowing for more dynamism. (Is that a word?)

Dim Northwind As new NorthwindDataContext
Dim query = Northwind.Products
.Where("CategoryID=2 And p.UnitPrice>3")
.OrderBy("SupplierID")
GridView1.DataSource = query
GridView1.DataBind()

Again, this works great when you don't know every input ahead of time. Marcin says:

DynamicQueryable is quite powerful and includes the following

  • Dynamic string-based querying of any LINQ provider (late-bound versions of Where, Select, OrderBy, Take, Skip, GroupBy, Any, and Count extension methods)
  • String-based mini expression language (like the “it” identifier in the sample below), including complex conditional statements and all operators
  • Dynamic creation of classes for projections

Now Marcin was able to rewrite my pile of Expression crap above into this luscious four line snippet. The DynamicQueryable magic is the "var result =" line.

protected void Page_Init(object sender, EventArgs e) {
var items = Column.Table.GetQuery();

var result = items.Select(Column.EntityTypeProperty.Name).Distinct().OrderBy("it");

foreach (var item in result) {
if (item != null) DropDownList1.Items.Add(item.ToString());
}
}

ScottGu also points to Joseph and Ben Albahari, authors of the C# 3.0 In a Nutshell book and their incredibly deep post on building type-safe predicate methods. Their PredicateBuilder is free in the LINQKit extension library and can really help out when you get even deeper into this topic.

Oh, and seriously, stop what you're doing now and go download LINQPad, the Albahari's most wonderful gift to us all. Then, thank them, and tell them how awesome they are.

Enjoy!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Hanselminutes on 9 - Follow up, 6 months later, On Managing People with Chris Sells

January 21, '10 Comments [6] Posted in Microsoft | Musings
Sponsored By
STO Ninja Logo - A square with a little Ninja Dude insdeSix months ago when my role changed a bit, I took Chris Sells to lunch at our local food court (where the lady at the Indian Restaurant always shakes the big piece of chicken off her serving spoon before she dishes my plate, but I'm not bitter) to talk about being a successful manager at Microsoft.

You might watch that short video from August of 2009 first, if you haven't seen it, before you watch this follow up.

I've led teams of developers on projects, but never done the whole HR thing in a large hierarchy. When I was Chief Architect at Corillian (about 450 people) I reported to the CTO but the VP of Engineering handled the human resources aspects of management and I focused on technology and strategy.

Chris leads a team at Microsoft and seems to get a lot done, so I figured it'd be a good idea. Check out our FIRST video interview from the August 2009 lunch here, then watch this FOLLOWUP Lunch from yesterday (about six months after the first) where I at first admit defeat, then go back into being in denial, then I leave to redouble my efforts. Enjoy the Wisdom of Chris Sells as he attempts to set me straight.

"Developers at Microsoft write code. Program Managers at Microsoft delete email.*"

Part 2 - Follow up with Chris Sells on Managing People - January 2010

 image

Part 1 - Chris Sells on Managing People - August 2009

Enjoy!

* Joke. We also move email into folders and schedule meetings.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

How many PCs in the world have the .NET Framework installed?

January 20, '10 Comments [43] Posted in ASP.NET | Learning .NET | Microsoft | Win7 | Windows Client | WPF
Sponsored By

image I did a second .NET Framework features informal poll recently, and as with all .NET related polls the question comes up: How many PCs have the .NET Framework on it?

If you're a company that is considering creating a client application using .NET (not Silverlight, but the .NET Framework) you'd probably like to know if your end-user needs to install something extra to use your app.

So I started asking questions. We've said things here and there about the pervasiveness of the .NET Framework but I wanted to get the final word (at the time of this writing) and put it somewhere easy to fine.

After some digging, here's what I've got:

  • Well over 90% of the PCs in the world have some version of the .NET Framework installed.
  • Over 65% of Windows PCs in the world have .NET 3.5 SP1 installed.

This is a lot higher than I thought, and it's pretty cool.

The .NET Framework is smaller than you'd think (that's why I wrote SmallestDotNet). The very small .NET 4 Client Profile makes it easier (both speed and download size) to put .NET on a machine.

I think these numbers will help folks who might be considering using .NET for a client application.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

NewsFlash: Computers are Faster Than Before - Importing Digital Video from Tape and Making a DVD

January 18, '10 Comments [14] Posted in Reviews | Tools | Win7
Sponsored By

Import video (2)The Wife and I travelled all over the world before we had kids (and all over the world after, actually...both boys have been on three continents) and amassed a huge pile of Video Tapes of our travels. I started with a Sony Digital 8 camera that recorded very crisp 640x480 uncompressed digital video to a standard Hi-8 tape, then "graduated" to a Mini-DV Canon camera, then most recently to a small Creative Vado HD. The Vado does 720p, but I figure next big family trip I'll talk to video prosumer Pete Brown and buy a real 1080p HD camera.

I just rediscovered a box full of these old video tapes. There's dozens of them. I remembered that I never made (slightly more permanent) DVDs or backups of these tapes because transferring video from the Digital 8 camera in 2003-2004 was a huge hassle. I have memories of messing about with firewire cards and drivers, camera timecodes and most significantly, hard drive space and CPU speed.

This tape-based digital video camera seems to pump out a gig of video per five minutes of tape, or about 20 gigs an hour. This was a big deal in 2004. Also, whatever machine I had 6 years ago had a lot of trouble keeping up and always dropped frames. I had to shut off services, background apps and defrag my hard drive because even the "tiniest bump in the road" meant a less-the-perfect transfer.

Even though I take video with my Vado HD - which saves to an internal 8gig flash memory - I basically gave up on tape=based video after this crappy experience, and dozens of tapes got put in a box. Today, his Sony DCR-TRV330 Digital Hi-8 camera still has fantastic quality and a high-quality lens (as if 6 years is an eternity).

This post is largely about the seamlessness of the process, much of which is due to everything working out of the box driver-wise in Windows 7, combined with the fact that computers are WAY faster now and hard drives are WAY larger. This of course, is hardly a newsflash, but I wanted to write this post because I was truly surprised at how this previously frustrating task actually became so easy that it was actually fun and more than a little rewarding.

Importing Video from a Digital Tape in 2010

I didn't know what to expect, but I figured what the heck, and I just turned the camera on, put in a tape and plugged it into my Windows 7 machine via a firewire cable.

Surprisingly, the camera was instantly recognized and even showed up in my Devices and Printers window (lower right).

Devices and Printers folder in Windows 7 - My Digital Video Camera shows up

A moment after the camera showed up here, this dialog conveniently popped up:

video1

I selected "Import the entire video" and "Import as multiple files." That last option will make multiple files if the there is more than 30 seconds of time elapsed between two cuts. This dialog and utility were included with Windows Live Photo Gallery, but they work so seamlessly, I though they were part of Windows.

Then, click Import...it automatically rewound the tape (nice touch) and started capturing video. The salient point here is that I didn't have to do anything other than plug it in and click OK.

 Import video

Machines today are so fast, I'm 20 minutes into this video, 35,000 frames and no drops. Piles of space free.

After the files are ripped, it was a 10 minute process to put together a nice menu, name some scenes and burn the DVD with Windows DVD Maker (which apparently comes with Windows, although a free Movie Editor requires a download).

This is so easy, I'm going to make DVDs of all these old video tapes, and I'll store the ripped video on my Windows Home Server.

It's nice to revisit things that were a problem or hassle a few years back and find they they are totally solved problems today.

Related Links

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Hanselminutes Podcast 194 - Hello World: Computer Programmer for Kids and Other Beginners

January 15, '10 Comments [11] Posted in Podcast | Programming
Sponsored By

51dJNsAaLuL._SL500_AA240_ My one-hundred-and-ninety-fourth podcast is up. Scott chats with Warren Sande and his 10 year old son, Carter, about their new book "Hello World: Computer Programming for Kids and Other Beginners." Listeners can get 40% off Hello World! from Manning.com with the code "hanselm40". The offer is valid until Jan 31, 2010

Subscribe: Subscribe to Hanselminutes Subscribe to my Podcast in iTunes

Download: MP3 Full Show

Links from the Show

Links from the Show

Do also remember the complete archives are always up and they have PDF Transcripts, a little known feature that show up a few weeks after each show.

Telerik is our sponsor for this show.

Check out their UI Suite of controls for ASP.NET. It's very hardcore stuff. One of the things I appreciate aboutTelerik is their commitment tocompleteness. For example, they have a page about their Right-to-Left support while some vendors have zero support, or don't bother testing. They also are committed to XHTML compliance and publish their roadmap. It's nice when your controls vendor is very transparent.

As I've said before this show comes to you with the audio expertise and stewardship of Carl Franklin. The name comes from Travis Illig, but the goal of the show is simple. Avoid wasting the listener's time. (and make the commute less boring)

Enjoy. Who knows what'll happen in the next show?

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.