<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/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>Designing LINQ operators</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/01/23/designing-linq-operators.aspx</link><description>I&amp;#39;ve started a small project (I&amp;#39;ll post a link when I&amp;#39;ve actually got something worthwhile to show) with some extra LINQ operators in - things which I think are missing from LINQ to Objects, basically. (I hope to include many of the ideas</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>http://beach.goodnanoav.com/</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/01/23/designing-linq-operators.aspx#1697173</link><pubDate>Mon, 29 Jun 2009 18:46:24 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1697173</guid><dc:creator>crurneturfink</dc:creator><description>&lt;p&gt;Questions and answers about Beach hotel southern california and On the beach poster &lt;a rel="nofollow" target="_new" href="http://beach.goodnanoav.com/"&gt;http://beach.goodnanoav.com/&lt;/a&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1697173" width="1" height="1"&gt;</description></item><item><title>re: Designing LINQ operators</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/01/23/designing-linq-operators.aspx#1673886</link><pubDate>Wed, 25 Feb 2009 21:43:16 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1673886</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;@Mike Rosenblum: The only reason to compile the expression is to execute it. If you pass in the parameter as well as the expression, you don&amp;#39;t need to execute it.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1673886" width="1" height="1"&gt;</description></item><item><title>re: Designing LINQ operators</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/01/23/designing-linq-operators.aspx#1673885</link><pubDate>Mon, 23 Feb 2009 13:19:43 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1673885</guid><dc:creator>Mike Rosenblum</dc:creator><description>&lt;p&gt;Jan, &lt;/p&gt;
&lt;p&gt;You might have seen it in Jon Fuller&amp;#39;s article here: &lt;a rel="nofollow" target="_new" href="http://jonfuller.codingtomusic.com/2008/12/11/static-reflection-method-guards/"&gt;jonfuller.codingtomusic.com/.../static-reflection-method-guards&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;But how would you do this so that you would not need to make use of .Compile()? I don&amp;#39;t quite follow you on that one. Could you show an example?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1673885" width="1" height="1"&gt;</description></item><item><title>Weekly Web Nuggets #49</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/01/23/designing-linq-operators.aspx#1673210</link><pubDate>Mon, 23 Feb 2009 03:30:40 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1673210</guid><dc:creator>Code Monkey Labs</dc:creator><description>&lt;p&gt;Pick of the week: The Sad Tragedy of Micro-Optimization Theater General Designing LINQ Operators : Jon Skeet has some good tips on writing LINQ operators. Mark a C# Class Data Member as &amp;lsquo;readonly&amp;rsquo; When It&amp;rsquo;s Read Only : Antonio Bello&lt;/p&gt;
&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1673210" width="1" height="1"&gt;</description></item><item><title>re: Designing LINQ operators</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/01/23/designing-linq-operators.aspx#1669273</link><pubDate>Thu, 05 Feb 2009 10:43:34 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1669273</guid><dc:creator>Jan de Wit</dc:creator><description>&lt;p&gt;Here&amp;#39;s a cool trick for getting the parameter name without duplicating it and making it visible to refactoring tools. It&amp;#39;s not mine, but I really can&amp;#39;t remember where I saw it...&lt;/p&gt;
&lt;p&gt;using System.Linq.Expressions;&lt;/p&gt;
&lt;p&gt;public static void ThrowIfNull&amp;lt;T&amp;gt;(Expression&amp;lt;Func&amp;lt;T&amp;gt;&amp;gt; expr) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Func&amp;lt;T&amp;gt; f = expr.Compile();&lt;/p&gt;
&lt;p&gt; &amp;nbsp;if (f() == null) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;var body = (MemberExpression) expr.Body; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;throw new ArgumentException(body.Member.Name + &amp;quot; is null&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Example: &lt;/p&gt;
&lt;p&gt;void test(string param) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;ThrowIfNull(() =&amp;gt; param);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;// do stuff with a non-null param&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;For efficiency, an overload could be added which takes the parameter, so we don&amp;#39;t need to compile the expression.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1669273" width="1" height="1"&gt;</description></item><item><title>Weekly Web Nuggets #49</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/01/23/designing-linq-operators.aspx#1667468</link><pubDate>Fri, 30 Jan 2009 21:39:05 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1667468</guid><dc:creator>Code Monkey Labs</dc:creator><description>&lt;p&gt;Pick of the week: The Sad Tragedy of Micro-Optimization Theater General Designing LINQ Operators : Jon Skeet has some good tips on writing LINQ operators. Mark a C# Class Data Member as ‘readonly’ When It’s Read Only : Antonio Bello explains the differences&lt;/p&gt;
&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1667468" width="1" height="1"&gt;</description></item><item><title>re: Designing LINQ operators</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/01/23/designing-linq-operators.aspx#1666066</link><pubDate>Mon, 26 Jan 2009 20:02:34 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1666066</guid><dc:creator>Stephen Cleary</dc:creator><description>&lt;p&gt;I agree that mutating operations should not be supported. C++ iterator categories did allow for this, but I never considered it a clean solution.&lt;/p&gt;
&lt;p&gt;As for lazily evaluating lists, I do it all the time. Yes, it&amp;#39;s more work than an iterator block, but with a common base class it&amp;#39;s not too bad. PowerCollections has one very similar to the one I use.&lt;/p&gt;
&lt;p&gt;I think I view LINQ as having more gaps than many people who have worked with it since it first came out. If LINQ is a way of saying &amp;quot;run this query on this input stream&amp;quot;, then it&amp;#39;s sufficient. But for me, I see LINQ as &amp;quot;execute these algorithms as the data streams through&amp;quot;, and when using that perspective, the gaps (both algorithm gaps and performance gaps) become more apparent.&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve been using these types of algorithms for ~10 years. Back then, I called them &amp;quot;pipe algorithms&amp;quot; after the Unix shell pipe (|), because it represented a similar concept: running a stream of data through a series of algorithms. C++ iterator categories and Python generators discovered the same concept, followed now by LINQ.&lt;/p&gt;
&lt;p&gt;All of these eventually run into what I call the &amp;quot;tee&amp;quot; problem; Unix developed process substitution with redirection to named pipes, I wrote my own rather complex system for C++ algorithms... and you&amp;#39;re developing &amp;quot;Push&amp;quot; LINQ.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1666066" width="1" height="1"&gt;</description></item><item><title>re: Designing LINQ operators</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/01/23/designing-linq-operators.aspx#1666049</link><pubDate>Mon, 26 Jan 2009 19:21:12 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1666049</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;@Stephen: Interesting ideas. Would you be thinking of a &amp;quot;Listable&amp;quot; then?&lt;/p&gt;
&lt;p&gt;The main problem I see with that is either a) it becomes a mutating operation (which is nasty - I didn&amp;#39;t include &amp;quot;don&amp;#39;t mutate the input&amp;quot; in the guidance above, because with IEnumerable you really don&amp;#39;t have to) or b) you have to return a whole list. I suppose that *could* be lazily evaluated, but that&amp;#39;s quite tricky - it certainly couldn&amp;#39;t be done with anything as simple as an iterator block.&lt;/p&gt;
&lt;p&gt;Of course, *some* LINQ query operators notice if they&amp;#39;re working with an IList - certainly Count() does. I&amp;#39;m not sure beyond that.&lt;/p&gt;
&lt;p&gt;Your point 2 could certainly be done quite easily though - an &amp;quot;OrderedIntersect&amp;quot; or something like that which streams both input sequences, and throws if it ever sees anything out of order. (That does require an IComparer rather than an IEqualityComparer, of course.)&lt;/p&gt;
&lt;p&gt;As for the &amp;quot;need&amp;quot; for it, I&amp;#39;m not sure. I&amp;#39;ve found quite a few little gaps, mostly in terms of &amp;quot;I want to apply a projection for the sake of finding the max, but I want the original value rather than the projected one&amp;quot; but I&amp;#39;ve never really felt the need for the kind of things you&amp;#39;re mentioning. That&amp;#39;s not to say they wouldn&amp;#39;t be useful though - I&amp;#39;m not the only developer using LINQ :)&lt;/p&gt;
&lt;p&gt;Thanks very much for your input - food for thought, certainly.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1666049" width="1" height="1"&gt;</description></item><item><title>re: Designing LINQ operators</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/01/23/designing-linq-operators.aspx#1666040</link><pubDate>Mon, 26 Jan 2009 18:55:17 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1666040</guid><dc:creator>Stephen Cleary</dc:creator><description>&lt;p&gt;Hi, Jon!&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve been a fan of your blog for some time, but thought I&amp;#39;d comment regarding your note on LINQ buffering.&lt;/p&gt;
&lt;p&gt;I have many years of background in C++ iterator/algorithm details as well as Python generators, and so I find LINQ to be particularly interesting as the C# equivalent of these concepts. In particular, I&amp;#39;ve given some thought to how buffering can be improved for the initial set of LINQ operations.&lt;/p&gt;
&lt;p&gt;There are two main problems for LINQ, as I see it:&lt;/p&gt;
&lt;p&gt;1) There&amp;#39;s no way to distinguish between sequential and random-access. You can kind of say that IEnumerable would imply sequential, while IList implies random-access; but this raises two other problems:&lt;/p&gt;
&lt;p&gt;1a) The random-access nature is not preserved through LINQ operators. i.e., Enumerable.Reverse does not return IList, even when applied on an IList.&lt;/p&gt;
&lt;p&gt;1b) LINQ operators do not look for random-access input sequences, so they choose less efficient algorithms. i.e., Reverse requires buffering even when passed an array.&lt;/p&gt;
&lt;p&gt;2) Some algorithms are not present in LINQ. e.g., an Intersect (or any other set-related operation) on already-ordered input sequences would be much more efficient.&lt;/p&gt;
&lt;p&gt;I believe (2) is largely the result of LINQ being done to provide &amp;quot;SQL in C#&amp;quot; rather than a general-purpose sequence algorithm framework.&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve played around with the idea of adding a bunch of LINQ operators in a library that would help to fill these gaps, and I believe it would require a new &amp;quot;LINQ namespace&amp;quot; (for lack of a better name - I&amp;#39;m referring to the facility provided by AsQueryable, AsParallel for PLINQ, etc). So, a new set of operators would work on IList interfaces.&lt;/p&gt;
&lt;p&gt;What do you think about the need for this?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1666040" width="1" height="1"&gt;</description></item><item><title>Designing LINQ operators - Jon Skeet: Coding Blog</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/01/23/designing-linq-operators.aspx#1665959</link><pubDate>Mon, 26 Jan 2009 06:43:25 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1665959</guid><dc:creator>DotNetShoutout</dc:creator><description>&lt;p&gt;Thank you for submitting this cool story - Trackback from DotNetShoutout&lt;/p&gt;
&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1665959" width="1" height="1"&gt;</description></item><item><title>re: Designing LINQ operators</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/01/23/designing-linq-operators.aspx#1665639</link><pubDate>Sat, 24 Jan 2009 21:14:21 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1665639</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;@Vincent: Oh yes. Reflector has to do a lot of clever work to guess at the source code. Did you use a lambda expression? An anonymous method? How do you tell the difference between for and foreach over an array? I bet there&amp;#39;s a ton of heuristics in there.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1665639" width="1" height="1"&gt;</description></item><item><title>re: Designing LINQ operators</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/01/23/designing-linq-operators.aspx#1665637</link><pubDate>Sat, 24 Jan 2009 21:11:44 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1665637</guid><dc:creator>Vincent Croquette</dc:creator><description>&lt;p&gt;Thanks Jon&lt;/p&gt;
&lt;p&gt;It&amp;#39;s in these moments I realise that creating a tool such as Reflector involves more than reflection skills&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1665637" width="1" height="1"&gt;</description></item><item><title>re: Designing LINQ operators</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/01/23/designing-linq-operators.aspx#1665602</link><pubDate>Sat, 24 Jan 2009 19:29:49 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1665602</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;@Vincent: Yes, I know that would be nice - but unless you did it in some post-compile step it&amp;#39;s basically impossible as far as I can tell.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1665602" width="1" height="1"&gt;</description></item><item><title>re: Designing LINQ operators</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/01/23/designing-linq-operators.aspx#1665591</link><pubDate>Sat, 24 Jan 2009 18:55:44 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1665591</guid><dc:creator>Vincent Croquette</dc:creator><description>&lt;p&gt;Yes, but I&amp;#39;d like to have&lt;/p&gt;
&lt;p&gt;x.ThrowIfNull()&lt;/p&gt;
&lt;p&gt;And the variable name &amp;quot;x&amp;quot; to be retrieved automatically for display purposes&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1665591" width="1" height="1"&gt;</description></item><item><title>re: Designing LINQ operators</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/01/23/designing-linq-operators.aspx#1665402</link><pubDate>Sat, 24 Jan 2009 09:40:17 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1665402</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;@Vincent: In fact in MiscUtil I have a ThrowIfNull extension method (or something like that) so I&amp;#39;d write:&lt;/p&gt;
&lt;p&gt;x.ThrowIfNull(&amp;quot;x&amp;quot;);&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1665402" width="1" height="1"&gt;</description></item><item><title>re: Designing LINQ operators</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/01/23/designing-linq-operators.aspx#1665389</link><pubDate>Sat, 24 Jan 2009 09:08:07 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1665389</guid><dc:creator>Vincent Croquette</dc:creator><description>&lt;p&gt;When .net newbies start using IEnumerable, they may think that Count is part of IEnumerable whithout realizing it is an extension method in the first place&lt;/p&gt;
&lt;p&gt;If you stick to the IEnumerable members, you should immediately realise you&amp;#39;re doing something odd&lt;/p&gt;
&lt;p&gt;BTW Jon, not related to this post but... I often write code like yours:&lt;/p&gt;
&lt;p&gt;if (source == null)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;throw new ArgumentNullException(&amp;quot;source&amp;quot;);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;especially when implementing WCF operations where I want to make sure the caller provides a valid data contract parameter&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve always wanted to be able to do something like:&lt;/p&gt;
&lt;p&gt;// Operation DoSomething&lt;/p&gt;
&lt;p&gt;public void DoSomething(ADataContract parameter)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;CheckOperationParameter(parameter);&lt;/p&gt;
&lt;p&gt; &amp;nbsp;...&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;private void CheckOperationParameter(object parameter)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;if (parameter == null)&lt;/p&gt;
&lt;p&gt; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;string localVariableName = ...&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;throw new FaultException&amp;lt;InvalidArgument&amp;gt;(localVariableName);&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Theoritically it should be possible to retrieve the local variable&amp;#39;s name using reflection but I haven&amp;#39;t found an easy way of doing it yet...&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1665389" width="1" height="1"&gt;</description></item></channel></rss>