<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jay R. Wren - lazy dawg evarlast &#187; linq</title>
	<atom:link href="http://jrwren.wrenfam.com/blog/tag/linq/feed/" rel="self" type="application/rss+xml" />
	<link>http://jrwren.wrenfam.com/blog</link>
	<description>babblings of a computer loving fool</description>
	<lastBuildDate>Wed, 01 Feb 2012 19:08:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>LINQ Abuse with the C# 4 dynamic type</title>
		<link>http://jrwren.wrenfam.com/blog/2010/03/04/linq-abuse-with-the-c-4-dynamic-type/</link>
		<comments>http://jrwren.wrenfam.com/blog/2010/03/04/linq-abuse-with-the-c-4-dynamic-type/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 03:13:48 +0000</pubDate>
		<dc:creator>jrwren</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[linq]]></category>

		<guid isPermaLink="false">http://jrwren.wrenfam.com/blog/2010/03/04/linq-abuse-with-the-c-4-dynamic-type/</guid>
		<description><![CDATA[With C# 4 adding some support for dynamic typing one of the first thing that I wanted to do is use it with LINQ. I want to do this: dynamic x; var h = from y in x where y &#8230; <a href="http://jrwren.wrenfam.com/blog/2010/03/04/linq-abuse-with-the-c-4-dynamic-type/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>With C# 4 adding some support for dynamic typing one of the first thing that I wanted to do is use it with LINQ.</p>
<p>I want to do this:</p>
<blockquote><p>dynamic x;     <br />var h = from y in x where y == 1 select y.something;</p>
</blockquote>
<p>But I get error messages on both where and select that says</p>
<blockquote><p><font color="#333333">Query expressions over source type ‘dynamic’ or with a join sequence of ‘dynamic’ are not allowed</font></p>
</blockquote>
<p>Major bummer.</p>
<p>But surely there is something I can do. <img src='http://jrwren.wrenfam.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>*the title of this post starts with LINQ abuse… please don’t comment about how stupid and evil this is. I know it. Instead, consider this an exercise in getting to know C# a little better.</p>
<p>The dynamic type is just sugar for the object type and some attributes to which the compiler pays attention.</p>
<p>Lets use object…</p>
<blockquote><p>object things = new[] { 0,1,2,3,4,5,6,7, };     <br />var whatIwant = from thing in things      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; where thing % 2 == 0      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; select thing;      <br />// or if you like longhand:      <br />var wiw = things.Where(thing =&gt; thing%2 == 0).Select(thing =&gt; thing);</p>
</blockquote>
<p>How does this compile? Well, by making Where and Select resolve to extension methods on object instead of extension methods on IEnumerable&lt;T&gt; (which is what people USUALLY think of when they think LINQ).</p>
<blockquote><p>public static IEnumerable&lt;dynamic&gt; Select(this object source, Func&lt;dynamic, dynamic&gt; map)     <br />{      <br />&#160;&#160;&#160; foreach (dynamic item in source as dynamic)      <br />&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; yield return map(item);      <br />&#160;&#160;&#160; }      <br />}      <br />public static IEnumerable&lt;dynamic&gt; Where(this object source, Func&lt;dynamic, dynamic&gt; predicate)      <br />{      <br />&#160;&#160;&#160; foreach (dynamic item in source as dynamic)      <br />&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (predicate(item))      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; yield return item;      <br />&#160;&#160;&#160; }      <br />}</p>
</blockquote>
<p>Extension methods on object, then cast to dynamic (extension methods aren’t allowed on dynamic).</p>
<p>It should be short work to fill out whatever LINQ methods are necessary to make whatever LINQ expressions you wish work against dynamic (object) and now you can use LINQ with a source that is typed dynamic.</p>
]]></content:encoded>
			<wfw:commentRss>http://jrwren.wrenfam.com/blog/2010/03/04/linq-abuse-with-the-c-4-dynamic-type/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

