<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>i have a framework... : Silverlight</title><link>http://weblogs.asp.net/freedomdumlao/archive/tags/Silverlight/default.aspx</link><description>Tags: Silverlight</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Building Functions vs. Building Expressions: Performance Comparison</title><link>http://weblogs.asp.net/freedomdumlao/archive/2009/02/23/building-functions-vs-building-expressions-performance-comparison.aspx</link><pubDate>Tue, 24 Feb 2009 04:47:35 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6924637</guid><dc:creator>fdumlao</dc:creator><slash:comments>0</slash:comments><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/freedomdumlao/commentapi.aspx?PostID=6924637</wfw:comment><comments>http://weblogs.asp.net/freedomdumlao/archive/2009/02/23/building-functions-vs-building-expressions-performance-comparison.aspx#comments</comments><description>&lt;p&gt;Yesterday I posted an entry describing a method for &lt;a title="C# Dynamic Function Factory" href="http://weblogs.asp.net/freedomdumlao/archive/2009/02/23/c-dynamic-function-factory.aspx" target="_blank"&gt;building functions as opposed to building expressions&lt;/a&gt; to get a more succinct syntax, and in general a more functional approach. There was a touch of reflection in there though, so I wasn’t too sure what the performance would be like.&lt;/p&gt;  &lt;p&gt;Even though the post was specifically about an alternative to building expressions, the commenter suggested that it was more efficient, based on his own benchmarks. I was shocked.&lt;/p&gt;  &lt;p&gt;Not being the type to believe something without seeing it, I found myself compelled to write my own simple benchmark and the results were shocking.&lt;/p&gt;  &lt;p&gt;Building functions is FASTER than building expressions. WAY faster. Let’s take a look:&lt;/p&gt;  &lt;h3&gt;The Code:&lt;/h3&gt;  &lt;p&gt;The function building method I created was this (again sorry for the formatting – I’m trying to make it fit):&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Func&amp;lt;T, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt; BuildEqFuncFor&amp;lt;T&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;    (&lt;span class="kwrd"&gt;string&lt;/span&gt; prop, &lt;span class="kwrd"&gt;object&lt;/span&gt; val)&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;return&lt;/span&gt; t =&amp;gt; t.GetType()&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;        .InvokeMember(prop, &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;            BindingFlags.GetProperty, &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;            &lt;span class="kwrd"&gt;null&lt;/span&gt;, &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;            t, &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;            &lt;span class="kwrd"&gt;null&lt;/span&gt;) == val;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;







.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;And the commenter suggested I use the approach where you build up an expression, and then compile it:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Func&amp;lt;T, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt; ExBuildEqFuncFor&amp;lt;T&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;    (&lt;span class="kwrd"&gt;string&lt;/span&gt; prop, &lt;span class="kwrd"&gt;object&lt;/span&gt; val)&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    var o = Expression.Parameter(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(T), &lt;span class="str"&gt;&amp;quot;t&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    Expression&amp;lt;Func&amp;lt;T, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; expression = &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        Expression.Lambda&amp;lt;Func&amp;lt;T, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt;(&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;            Expression.Equal(&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;                Expression.PropertyOrField(o, prop), &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;                Expression.Constant(val)), o);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;    &lt;span class="kwrd"&gt;return&lt;/span&gt; expression.Compile();&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;







.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;h3&gt;The Results:&lt;/h3&gt;

&lt;p&gt;I ran each method 1, 100, and 10,000 times and here were the results (in seconds):&lt;/p&gt;

&lt;p&gt;&lt;img title="buildfuncVSbuildexpBenchmarks" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="559" alt="buildfuncVSbuildexpBenchmarks" src="http://weblogs.asp.net/blogs/freedomdumlao/buildfuncVSbuildexpBenchmarks_250B1A14.png" width="373" border="0" /&gt; &lt;/p&gt;

&lt;p&gt;It’s unbelievable how much faster the function building method ran, compared to creating an expression tree and compiling it. In fact, within 100 runs there was almost no change at all!&lt;/p&gt;

&lt;p&gt;I looked into what was making the expression building function so long to create, and as I suspected it was the “Compile” method call that is used to turn the whole tree into a function. I changed the method to just return the expression and not compile it and re-ran the tests. &lt;strong&gt;&lt;em&gt;It still took twice as long just to build the expression as it did to build an actual function.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you think about it though, it makes sense. Compiling on the fly was never supposed to be considered a performant (yes I said “performant”) method of metaprogramming in C# and if you are going to use it, it would be wise to implement some caching to prevent the need to constantly build the same functions again and again.&lt;/p&gt;

&lt;p&gt;Or, you can just build the functions directly.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6924637" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/LINQ+to+SQL/default.aspx">LINQ to SQL</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/Web+Services/default.aspx">Web Services</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/WCF/default.aspx">WCF</category></item><item><title>C# Dynamic Function Factory</title><link>http://weblogs.asp.net/freedomdumlao/archive/2009/02/23/c-dynamic-function-factory.aspx</link><pubDate>Mon, 23 Feb 2009 06:36:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6923976</guid><dc:creator>fdumlao</dc:creator><slash:comments>4</slash:comments><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/freedomdumlao/commentapi.aspx?PostID=6923976</wfw:comment><comments>http://weblogs.asp.net/freedomdumlao/archive/2009/02/23/c-dynamic-function-factory.aspx#comments</comments><description>&lt;p&gt;Building functions &lt;em&gt;instead&lt;/em&gt; of expression trees.&lt;/p&gt;  &lt;p&gt;The functional programming features of the latest versions of C# allow developers to harness a very expressive programming paradigm to solve challenges in new, elegant ways. Two of the very interesting new features are the “Func&amp;lt;&amp;gt;” delegate and the Lamda expression.&lt;/p&gt;  &lt;p&gt;A Lamda expression is a simple expression that is interpreted by the compiler as either a delegate or an expression tree, depending on the Type called for in the given context. You see them used often in LINQ. For example:&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;var bostonPeople = people.Where( p =&amp;gt; p.City == &lt;span class="str"&gt;&amp;quot;Boston&amp;quot;&lt;/span&gt; );&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;








.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;Some time has been devoted in explaining how a LINQ query like the above can be created dynamically by building expression trees (see the excellent article: &lt;a title="Creating Dynamic Queries in LINQ" href="http://srtsolutions.com/blogs/billwagner/archive/2007/11/20/creating-dynamic-queries-in-linq.aspx" target="_blank"&gt;Creating Dynamic Queries in LINQ&lt;/a&gt; by Bill Wagner). While this approach to creating a Lambda expression dynamically is very powerful, some may find that it is far more than they need and instead could get by with a simpler implementation. Others may find it difficult to read or understand and therefore provides a challenge in maintenance.&lt;/p&gt;

&lt;p&gt;One possible solution is to use the powerful new Func&amp;lt;&amp;gt; and a lambda expression to easily create a function on the fly. This solution is not quite as powerful as building an expression tree but in situations where it’s possible it will save quite a bit of code.&lt;/p&gt;

&lt;p&gt;To demonstrate, how about a completely contrived example. Let’s say you have this object in your domain:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; Person&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name { get; set; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; City { get; set; }&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; State { get; set; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;








.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;and then let’s assume you need to have a way to return back a subset of a collection of “people” based on some criteria that won’t be known until runtime. We can create a factory that builds a function on the fly that we can pass into a LINQ method. Essentially what the “BuildEqFuncFor&amp;lt;T&amp;gt;” method does is build a Func&amp;lt;T, bool&amp;gt; that compares a given value against a named property of T. The signature of the built function is compatible with most LINQ query parameter expectations. (Please excuse the code formatting and naming conventions – I’m trying to fit it into this post.) &lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; FunctionFactory&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Func&amp;lt;T, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt; BuildEqFuncFor&amp;lt;T&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;        (&lt;span class="kwrd"&gt;string&lt;/span&gt; prop, &lt;span class="kwrd"&gt;object&lt;/span&gt; val)&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; t =&amp;gt; t.GetType()&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;            .InvokeMember(prop, &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;                BindingFlags.GetProperty, &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;                &lt;span class="kwrd"&gt;null&lt;/span&gt;, &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;                t, &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;                &lt;span class="kwrd"&gt;null&lt;/span&gt;) == val;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;    }&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;







.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;Now this factory only supports building functions that test for equivalency between some property of some object and a value, however more advanced functions could be constructed using the same concept.&lt;/p&gt;

&lt;p&gt;We can now use this FunctionFactory to create a function on the fly and use it in a LINQ method to query our people collection. Let’s see what that looks like:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;var personIsFromBoston = &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;    FunctionFactory.BuildEqFuncFor&amp;lt;Person&amp;gt;(&lt;span class="str"&gt;&amp;quot;City&amp;quot;&lt;/span&gt;, &lt;span class="str"&gt;&amp;quot;Boston&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;var bostonPeople = people.Where( personIsFromBoston );&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;&lt;span class="kwrd"&gt;if&lt;/span&gt; (bostonPeople.Any())&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    bostonPeople.ToList().ForEach(p =&amp;gt; Console.WriteLine(p.Name));&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;





.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;Obviously this is a simple example but it could easily be expanded upon (and optimized) to create a more advanced function factory that could generate functions to solve problems beyond just creating dynamic LINQ queries. An issue you should take into account, is there is no compile time type checking so you should take care to handle cases where the type of the property doesn’t match the type of the value, or that the property even exists on the type provided at all.&lt;/p&gt;

&lt;p&gt;As an interesting aside… this could be made even a bit more flexible by adding another function layer:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;Func&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, Func&amp;lt;Person, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; personIsFrom =&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;    s =&amp;gt; FunctionFactory.BuildEqFuncFor&amp;lt;Person&amp;gt;(&lt;span class="str"&gt;&amp;quot;City&amp;quot;&lt;/span&gt;, s);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;var bostonPeople = people.Where( personIsFrom(&lt;span class="str"&gt;&amp;quot;Boston&amp;quot;&lt;/span&gt;) );&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;I hope you found this interesting, I look forward to your comments.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6923976" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/LINQ+to+SQL/default.aspx">LINQ to SQL</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/Orcas/default.aspx">Orcas</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/Web+Services/default.aspx">Web Services</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/WCF/default.aspx">WCF</category></item><item><title>Silverlight Adaptive Streaming: How it works</title><link>http://weblogs.asp.net/freedomdumlao/archive/2008/11/03/silverlight-adaptive-streaming-how-it-works.aspx</link><pubDate>Mon, 03 Nov 2008 16:11:12 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6718052</guid><dc:creator>fdumlao</dc:creator><slash:comments>3</slash:comments><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/freedomdumlao/commentapi.aspx?PostID=6718052</wfw:comment><comments>http://weblogs.asp.net/freedomdumlao/archive/2008/11/03/silverlight-adaptive-streaming-how-it-works.aspx#comments</comments><description>&lt;p&gt;Recently an &lt;a href="http://www.akamai.com/html/about/press/releases/2008/press_102808.html" target="_blank"&gt;announcement was made&lt;/a&gt; by Akamai that it was partnering with Microsoft to provide an Adaptive Streaming solution for Silverlight and IIS 7.0. Since I work in the online video industry I found the announcement very interesting, especially considering &lt;a href="http://www.movenetworks.com/news-releases/move-networks-to-enter-into-strategic-relationship-with-microsoft" target="_blank"&gt;Move Network's previous announcement&lt;/a&gt; that it had formed a &amp;quot;Strategic Releationship&amp;quot; with Microsoft to provide this exact functionality.&lt;/p&gt;  &lt;p&gt;I'm not sure if this represents the fruits of this relationship or not, but I can say that the two technologies are extremely similar in how they provide an almost &amp;quot;instant-on&amp;quot;, high-definition stream of video to the client. Which brings me to the &amp;quot;meat&amp;quot; of this article - how the heck to they do that?&lt;/p&gt;  &lt;p&gt;Specifically, you can see an example of what I'm talking about at &lt;a href="http://www.smoothHD.com"&gt;www.smoothHD.com&lt;/a&gt;. You'll need Silverlight of course, but once you do you'll notice that the time it takes the video to begin playing is almost negligible and the quality is outstanding... unless you have a crappy connection... in which case you'll notice that it starts up quickly and has so-so to poor quality; which is by design. This is because Adaptive Streaming &amp;quot;adapts&amp;quot; to your bandwidth so that you get the best experience available to you. The thought is that it would be better for a client with slower access to get &amp;quot;something&amp;quot; rather than &amp;quot;buffering&amp;quot;. &lt;/p&gt;  &lt;h4&gt;Step By Step - What makes this thing tick?&lt;/h4&gt;  &lt;p&gt;Let me start with the caveat that this is a high level overview of the technology - it's not perfect, but should provide you the jist of it. Now that I've said that, let's get started.&lt;/p&gt;  &lt;p&gt;It all starts with encoding. In order to provide users with a stream appropriate for their bandwidth, the video needs to be encoded at various bitrates from low to very high quality. The more versions created the better the stream will be able to adapt.&lt;/p&gt;  &lt;p&gt;Besides just creating several versions, the video needs to be &amp;quot;cut up&amp;quot; into many pieces. Most likely, this is done by cutting at particular times or frames in the video instead of cutting at particular sizes. So for example, if my video was encoded at 3 different bitrates (by the way 3 is far to few but makes my example easier) I might slice each of them at 5 second intervals ( I would probably make the interval shorter than that but again, this is an example). This way, the first slice of the video encoded using bitrate A would be 2kb, the first slice of the one using bitrate B would be 5kb, and the first slice of bitrate C would be 8kb. They are all different sizes but contain the exact same portion of video as their peers.&lt;/p&gt;  &lt;p&gt;This is where Akamai comes in (although you could likely use any modern CDN). You need to ensure that access to these video &amp;quot;pieces&amp;quot; is fast, reliable, etc... So using a CDN like Akamai puts the files closer to the video consumer making delivery problems less likely. Once the video is on CDN, it's all up to the player.&lt;/p&gt;  &lt;p&gt;The player bears the brunt of the work of making sure that the video stays smooth regardless of network conditions. The first thing that happens when the player starts up, is it grabs a manifest file containing information about all of those video pieces. It then makes a quick determination of the user's network bandwidth and grabs the first piece of video that is appropriate for the client's connection and begins playing. As that first piece begins playing, it looks closer at the bandwidth of the client and then adjusts the video quality when it grabs the next piece of video. It will continue to monitor the client's performance as it plays, and adjusting which piece of video to grab based on the information it has most recently gathered.&lt;/p&gt;  &lt;p&gt;If you look at what it's doing here, you'll see that we aren't really talking about a &amp;quot;stream&amp;quot; but instead the player is playing small video files in a seamless sequence so that there is no interruption to the client. Because the video files are very small they load quickly and don't require waiting for &amp;quot;buffering&amp;quot;. The video can begin playing when the first &amp;quot;piece&amp;quot; is downloaded (even before if the codec is one that allows for partial file playback). This also allows a client to &amp;quot;jump&amp;quot; to another spot in the video without having to wait for the player to re-buffer, because it just has to grab the small video file at the point you want to jump to.&lt;/p&gt;  &lt;p&gt;One cool thing to note here; if you think about how the Internet works, you'll realize that there are many many more significant benefits to this than just smooth video for clients. As more and more clients watch a particular video, proxy caches all across the Internet will cache these small &amp;quot;pieces&amp;quot; of video files (as they tend to do with smaller files that are requested frequently). In essence these proxies become an extension of your CDN, except that it's free; because the proxies will serve up these small cached files without ever talking to Akamai or whatever CDN you are working with to deliver your video. &lt;/p&gt;  &lt;p&gt;Hopefully this has provided some insight for those of you who wondered about this technology - I am excited to see more of this coming soon.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6718052" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/Akamai/default.aspx">Akamai</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/CDN/default.aspx">CDN</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/IIS/default.aspx">IIS</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/Streaming/default.aspx">Streaming</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/Video/default.aspx">Video</category><category domain="http://weblogs.asp.net/freedomdumlao/archive/tags/Web+Services/default.aspx">Web Services</category></item></channel></rss>