<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>meek</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/meek/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/meek/feed.xml</link>
    <description>crossing the streams</description>
    <lastBuildDate>Sat, 11 Apr 2015 20:03:02 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Dynamic Union</title>
      <link>https://docs.microsoft.com/archive/blogs/meek/dynamic-union</link>
      <pubDate>Thu, 12 Apr 2012 11:09:00 GMT</pubDate>
      <dc:creator><![CDATA[Colin Meek]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/meek/2012/04/12/dynamic-union/</guid>
      <description><![CDATA[Let&rsquo;s say there are several agents &ndash; e.g. devices &ndash; producing temporal streams. It...]]></description>
      <content:encoded><![CDATA[<p>Let&rsquo;s say there are several agents &ndash; e.g. devices &ndash; producing temporal streams. It may be interesting to merge these sequences into a single stream that can be processed by StreamInsight. The &ldquo;union&rdquo; operator allows you to merge a fixed number of inputs, but what happens when inputs come and go over time? Some policy is needed that can make sense of these dynamic inputs since they may disagree on how time is progressing. A &ldquo;correct&rdquo; policy that doesn&rsquo;t propagate a CTI that might later be violated is no good: some new input can always come on line with an earlier CTI value so such a policy doesn&rsquo;t allow any CTIs through. A &ldquo;loose&rdquo; policy that propagates all CTIs is useless because it assumes that all inputs are progressing in lockstep &ndash; implausible given network latency, clock drift, etc.</p>
<p><b><i>Temporal stream</i></b><i>: a sequence of timestamped events. An event may be a Current Time Increment (CTI) event. A CTI is a promise that no subsequent events will have timestamps lower than that of the CTI.</i></p>
<p>A possible Goldilocks policy: allow for some maximum deviation between the most and least advanced input (let&rsquo;s call this timespan the <i>delay</i>). Whenever a CTI event from any input is processed &ndash; and whenever that CTI is greater than any seen so far &ndash; subtract the delay so that the CTI doesn&rsquo;t need to disqualify events from a less advanced input. If after delaying the CTI an incoming event still violates the CTI, drop or adjust the event.</p>
<h3>Transform Subject</h3>
<p>The ISubject&lt;,&gt; contract allows us to encapsulate the policy described above. Multiple producers can feed the subject (via calls to On*). Each producer can come and go at its own pace. How can we implement such a subject? Let&rsquo;s start with a helper factory method that allows us to create a &ldquo;transform subject&rdquo;. This subject applies an arbitrary function to an input observable, where that function may represent a stateful computation.</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: 'Courier New', courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: #f4f4f4;" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum1"> 1:</span> <span style="color: #0000ff;">using</span> System;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum2"> 2:</span> <span style="color: #0000ff;">using</span> System.Reactive.Disposables;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum3"> 3:</span> <span style="color: #0000ff;">using</span> System.Reactive.Linq;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum4"> 4:</span> <span style="color: #0000ff;">using</span> System.Reactive.Subjects;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum5"> 5:</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">class</span> TransformSubject</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum6"> 6:</span> {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum7"> 7:</span>     <span style="color: #008000;">/// &lt;summary&gt;</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum8"> 8:</span>     <span style="color: #008000;">/// Creates subject that supports transformation of incoming events.</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum9"> 9:</span>     <span style="color: #008000;">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum10"> 10:</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> ISubject&lt;TIn, TOut&gt; Create&lt;TIn, TOut&gt;(Func&lt;IObservable&lt;TIn&gt;, IObservable&lt;TOut&gt;&gt; transform)</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum11"> 11:</span>     {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum12"> 12:</span>         <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">null</span> == transform) { <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> ArgumentNullException(<span style="color: #006080;">"transform"</span>); }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum13"> 13:</span>         <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> _&lt;TIn, TOut&gt;(transform);</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum14"> 14:</span>     }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum15"> 15:</span>     <span style="color: #0000ff;">sealed</span> <span style="color: #0000ff;">class</span> _&lt;TIn, TOut&gt; : ISubject&lt;TIn, TOut&gt;, IDisposable</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum16"> 16:</span>     {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum17"> 17:</span>         <span style="color: #0000ff;">readonly</span> IObserver&lt;TIn&gt; input;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum18"> 18:</span>         <span style="color: #0000ff;">readonly</span> IObservable&lt;TOut&gt; output;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum19"> 19:</span>         <span style="color: #0000ff;">readonly</span> IDisposable subscription;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum20"> 20:</span>         <span style="color: #0000ff;">public</span> _(Func&lt;IObservable&lt;TIn&gt;, IObservable&lt;TOut&gt;&gt; transform)</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum21"> 21:</span>         {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum22"> 22:</span>             var inputSubject = <span style="color: #0000ff;">new</span> Subject&lt;TIn&gt;();</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum23"> 23:</span>             var outputConnectable = transform(inputSubject).Publish();</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum24"> 24:</span>             <span style="color: #0000ff;">this</span>.subscription = <span style="color: #0000ff;">new</span> CompositeDisposable(inputSubject, outputConnectable.Connect());</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum25"> 25:</span>             <span style="color: #0000ff;">this</span>.input = inputSubject;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum26"> 26:</span>             <span style="color: #0000ff;">this</span>.output = outputConnectable;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum27"> 27:</span>         }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum28"> 28:</span>         <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> OnCompleted()</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum29"> 29:</span>         {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum30"> 30:</span>             <span style="color: #0000ff;">this</span>.input.OnCompleted();</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum31"> 31:</span>         }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum32"> 32:</span>         <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> OnError(Exception error)</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum33"> 33:</span>         {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum34"> 34:</span>             <span style="color: #0000ff;">this</span>.input.OnError(error);</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum35"> 35:</span>         }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum36"> 36:</span>         <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> OnNext(TIn <span style="color: #0000ff;">value</span>)</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum37"> 37:</span>         {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum38"> 38:</span>             <span style="color: #0000ff;">this</span>.input.OnNext(<span style="color: #0000ff;">value</span>);</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum39"> 39:</span>         }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum40"> 40:</span>         <span style="color: #0000ff;">public</span> IDisposable Subscribe(IObserver&lt;TOut&gt; observer)</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum41"> 41:</span>         {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum42"> 42:</span>             <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">this</span>.output.Subscribe(observer);</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum43"> 43:</span>         }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum44"> 44:</span>         <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> Dispose()</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum45"> 45:</span>         {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum46"> 46:</span>             <span style="color: #0000ff;">this</span>.subscription.Dispose();</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum47"> 47:</span>         }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum48"> 48:</span>     }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum49"> 49:</span> }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum50"> 50:</span>&nbsp; </pre>
<!--CRLF--></div>
</div>
<p><span style="font-family: Arial;" face="Arial">Notice that this subject encapsulates two other subjects. One represents the input, a plain old Subject&lt;&gt; that allows input events to be passed to the transform logic. The other represents the output, a connectable observable that allows multiple consumers to read the output of the transform. When the subject is disposed, both the input subject and the output connection are released. A simple example of a transform subject that adds one to every incoming integer:</span></p>
<div id="codeSnippetWrapper">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;" id="codeSnippet">TransformSubject.Create((IObservable&lt;<span style="color: #0000ff;">int</span>&gt; xs) =&gt; xs.Select(x =&gt; x + 1))</pre>
</div>
<h3>CTI Synchronizing Subject (Round 1)</h3>
<p>We can create a &ldquo;CTI synchronizing&rdquo; subject by constructing a transform subject where the transform logic tracks the highest CTI seen so far and modifies CTIs and drops events as needed:</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: 'Courier New', courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: #f4f4f4;" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum1"> 1:</span> <span style="color: #0000ff;">using</span> System;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum2"> 2:</span> <span style="color: #0000ff;">using</span> System.Reactive.Linq;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum3"> 3:</span> <span style="color: #0000ff;">using</span> System.Reactive.Subjects;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum4"> 4:</span> <span style="color: #0000ff;">using</span> Microsoft.ComplexEventProcessing;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum5"> 5:</span>  </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum6"> 6:</span> <span style="color: #008000;">/// &lt;summary&gt;</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum7"> 7:</span> <span style="color: #008000;">/// The subject takes as input zero or more temporal streams. It outputs a temporal stream by applying</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum8"> 8:</span> <span style="color: #008000;">/// a delay to all incoming CTIs and dropping any events that violate the CTI. The delay represents the</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum9"> 9:</span> <span style="color: #008000;">/// maximum expected divergence between the most advanced and least advanced input stream.</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum10"> 10:</span> <span style="color: #008000;">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum11"> 11:</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">class</span> CtiSynchronizingSubject</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum12"> 12:</span> {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum13"> 13:</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> ISubject&lt;PointEvent&lt;T&gt;, PointEvent&lt;T&gt;&gt; CreatePoint&lt;T&gt;(TimeSpan delay)</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum14"> 14:</span>     {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum15"> 15:</span>         <span style="color: #0000ff;">return</span> Create&lt;PointEvent&lt;T&gt;, T&gt;(delay, p =&gt; p.StartTime, t =&gt; PointEvent&lt;T&gt;.CreateCti(t));</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum16"> 16:</span>     }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum17"> 17:</span>  </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum18"> 18:</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> ISubject&lt;IntervalEvent&lt;T&gt;, IntervalEvent&lt;T&gt;&gt; CreateInterval&lt;T&gt;(TimeSpan delay)</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum19"> 19:</span>     {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum20"> 20:</span>         <span style="color: #0000ff;">return</span> Create&lt;IntervalEvent&lt;T&gt;, T&gt;(delay, i =&gt; i.StartTime, t =&gt; IntervalEvent&lt;T&gt;.CreateCti(t));</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum21"> 21:</span>     }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum22"> 22:</span>  </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum23"> 23:</span>     <span style="color: #008000;">// Edge events are not currently implemented. Care is needed to avoid unmatched start edges in the output</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum24"> 24:</span>     <span style="color: #008000;">// stream.</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum25"> 25:</span>  </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum26"> 26:</span>     <span style="color: #0000ff;">static</span> ISubject&lt;TEvent, TEvent&gt; Create&lt;TEvent, TPayload&gt;(TimeSpan delay, Func&lt;TEvent, DateTimeOffset&gt; getApplicationTime, Func&lt;DateTimeOffset, TEvent&gt; createCti)</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum27"> 27:</span>         <span style="color: #0000ff;">where</span> TEvent : TypedEvent&lt;TPayload&gt;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum28"> 28:</span>     {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum29"> 29:</span>         <span style="color: #0000ff;">if</span> (delay &lt; TimeSpan.Zero) { <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> ArgumentOutOfRangeException(<span style="color: #006080;">"delay"</span>, <span style="color: #006080;">"Delay must be non-negative."</span>); }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum30"> 30:</span>  </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum31"> 31:</span>         <span style="color: #0000ff;">return</span> TransformSubject.Create((IObservable&lt;TEvent&gt; input) =&gt;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum32"> 32:</span>         { </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum33"> 33:</span>             <span style="color: #008000;">// Delay is applied to all CTIs to allow less advanced inputs to contribute events without violating</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum34"> 34:</span>             <span style="color: #008000;">// CTIs from more advanced inputs.</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum35"> 35:</span>             var delayed = from e <span style="color: #0000ff;">in</span> input</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum36"> 36:</span>                           select e.EventKind == EventKind.Cti</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum37"> 37:</span>                               ? createCti(SafeSubtract(getApplicationTime(e), delay))</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum38"> 38:</span>                               : e;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum39"> 39:</span>  </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum40"> 40:</span>             <span style="color: #008000;">// The highest (delayed) CTI seen so far is tracked in the Scan accumulator so that only CTIs that advance time</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum41"> 41:</span>             <span style="color: #008000;">// can be emitted and so that events that violate those CTIs can be dropped.</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum42"> 42:</span>             var withHighCtis = delayed.Synchronize().Scan(</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum43"> 43:</span>                 <span style="color: #0000ff;">new</span> </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum44"> 44:</span>                 { </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum45"> 45:</span>                     HighCti = <span style="color: #0000ff;">default</span>(DateTimeOffset), </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum46"> 46:</span>                     Evt = <span style="color: #0000ff;">default</span>(TEvent), </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum47"> 47:</span>                 },</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum48"> 48:</span>                 (accumulator, evt) =&gt; <span style="color: #0000ff;">new</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum49"> 49:</span>                 {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum50"> 50:</span>                     HighCti = evt.EventKind != EventKind.Cti ? accumulator.HighCti : Max(accumulator.HighCti, getApplicationTime(evt)),</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum51"> 51:</span>                     Evt = evt,</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum52"> 52:</span>                 });</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum53"> 53:</span>  </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum54"> 54:</span>             <span style="color: #0000ff;">return</span> from t <span style="color: #0000ff;">in</span> withHighCtis</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum55"> 55:</span>                    <span style="color: #0000ff;">where</span> t.HighCti &lt;= getApplicationTime(t.Evt)</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum56"> 56:</span>                    select t.Evt;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum57"> 57:</span>         });</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum58"> 58:</span>     }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum59"> 59:</span>  </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum60"> 60:</span>     <span style="color: #0000ff;">static</span> DateTimeOffset SafeSubtract(DateTimeOffset x, TimeSpan y)</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum61"> 61:</span>     {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum62"> 62:</span>         <span style="color: #0000ff;">return</span> x.UtcTicks &gt;= y.Ticks ? x - y : DateTimeOffset.MinValue;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum63"> 63:</span>     }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum64"> 64:</span>  </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum65"> 65:</span>     <span style="color: #0000ff;">static</span> DateTimeOffset Max(DateTimeOffset x, DateTimeOffset y)</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum66"> 66:</span>     {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum67"> 67:</span>         <span style="color: #0000ff;">return</span> x &gt; y ? x : y;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum68"> 68:</span>     }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum69"> 69:</span> }</pre>
<!--CRLF--></div>
</div>
<p>In case you haven&rsquo;t encountered them before, a couple of Rx operators are worth calling out. First, we apply the <a href="http://msdn.microsoft.com/en-us/library/ff403569(v=VS.92).aspx">Synchronize</a> operator which serializes all incoming events &ndash; allows us to avoid any race conditions due to inputs running on different threads. Second, the <a href="http://msdn.microsoft.com/en-us/library/hh212007(v=vs.103).aspx">Scan</a> operator allows us to define an accumulator that tracks the highest CTI seen so far. If an event has a timestamp that precedes the highest CTI seen so far, it must be dropped to avoid a CTI violation.</p>
<p>Let&rsquo;s look at how the subject behaves given interleaved events from two simulated input sources:</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: 'Courier New', courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: #f4f4f4;" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum1"> 1:</span> var startTime = <span style="color: #0000ff;">new</span> DateTimeOffset(DateTime.Today);</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum2"> 2:</span>&nbsp; </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum3"> 3:</span> <span style="color: #008000;">// create sync subject (and monitor its output)</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum4"> 4:</span> var ctiSyncSubject = CtiSynchronizingSubject.CreatePoint&lt;<span style="color: #0000ff;">double</span>&gt;(TimeSpan.FromMinutes(5));</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum5"> 5:</span>  </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum6"> 6:</span> <span style="color: #0000ff;">using</span> (ctiSyncSubject.Subscribe(p =&gt;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum7"> 7:</span> {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum8"> 8:</span>        <span style="color: #0000ff;">int</span> t = (p.StartTime - startTime).Minutes;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum9"> 9:</span>        <span style="color: #0000ff;">if</span> (p.EventKind == EventKind.Cti) Console.WriteLine(<span style="color: #006080;">"OUTPUT CTI: time {0}"</span>, t);</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum10"> 10:</span>        <span style="color: #0000ff;">else</span> Console.WriteLine(<span style="color: #006080;">"OUTPUT Insert: time {0}, payload {1}"</span>, t, p.Payload);</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum11"> 11:</span> }))</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum12"> 12:</span> {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum13"> 13:</span>        <span style="color: #008000;">// helpers feeding input to the subject </span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum14"> 14:</span>        Action&lt;<span style="color: #0000ff;">string</span>, <span style="color: #0000ff;">int</span>, <span style="color: #0000ff;">double</span>&gt; insert = (l, t, p) =&gt;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum15"> 15:</span>        {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum16"> 16:</span>               Console.WriteLine(<span style="color: #006080;">"INPUT Insert: input {0}, time {1}, payload {2}"</span>, l, t, p);</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum17"> 17:</span>               ctiSyncSubject.OnNext(PointEvent.CreateInsert(startTime.AddMinutes(t), p));</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum18"> 18:</span>        };</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum19"> 19:</span>        Action&lt;<span style="color: #0000ff;">string</span>, <span style="color: #0000ff;">int</span>&gt; cti = (l, t) =&gt;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum20"> 20:</span>        {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum21"> 21:</span>               Console.WriteLine(<span style="color: #006080;">"INPUT CTI: input {0}, time {1}"</span>, l, t);</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum22"> 22:</span>               ctiSyncSubject.OnNext(PointEvent.CreateCti&lt;<span style="color: #0000ff;">double</span>&gt;(startTime.AddMinutes(t)));</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum23"> 23:</span>        };</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum24"> 24:</span>        </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum25"> 25:</span>        <span style="color: #008000;">// simulate interleaved input from two sources where input "A" is ahead of input "B"</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum26"> 26:</span>        insert(<span style="color: #006080;">"A"</span>, 5, 1.0);</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum27"> 27:</span>        insert(<span style="color: #006080;">"B"</span>, 0, 2.0);</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum28"> 28:</span>        cti(<span style="color: #006080;">"A"</span>, 5);</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum29"> 29:</span>        cti(<span style="color: #006080;">"B"</span>, 0);</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum30"> 30:</span>        insert(<span style="color: #006080;">"A"</span>, 7, 3.0);</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum31"> 31:</span>        cti(<span style="color: #006080;">"A"</span>, 8);</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum32"> 32:</span>        insert(<span style="color: #006080;">"B"</span>, 1, 4.0);</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum33"> 33:</span> }</pre>
<!--CRLF--></div>
</div>
<p>&nbsp;</p>
<p>The following output is produced:</p>
<blockquote>
<p><span style="font-family: Consolas;" face="Consolas">INPUT Insert: input A, time 5, payload 1 <br />OUTPUT Insert: time 5, payload 1 <br />INPUT Insert: input B, time 0, payload 2 <br />OUTPUT Insert: time 0, payload 2 <br />INPUT CTI: input A, time 5 <br />OUTPUT CTI: time 0 <br />INPUT CTI: input B, time 0 <br />INPUT Insert: input A, time 7, payload 3 <br />OUTPUT Insert: time 7, payload 3 <br />INPUT CTI: input A, time 8 <br />OUTPUT CTI: time 3 <br />INPUT Insert: input B, time 1, payload 4</span></p>
</blockquote>
<h3>CTI Synchronizing Subject (Round 2)</h3>
<p>A different implementation of the subject that is written in a more imperative style now. Instead of using the Scan operator to maintain state, a custom accumulator class is used:</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: 'Courier New', courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: #f4f4f4;" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum1"> 1:</span> <span style="color: #0000ff;">using</span> System;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum2"> 2:</span> <span style="color: #0000ff;">using</span> System.Reactive.Linq;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum3"> 3:</span> <span style="color: #0000ff;">using</span> System.Reactive.Subjects;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum4"> 4:</span> <span style="color: #0000ff;">using</span> Microsoft.ComplexEventProcessing;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum5"> 5:</span>  </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum6"> 6:</span> <span style="color: #008000;">/// &lt;summary&gt;</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum7"> 7:</span> <span style="color: #008000;">/// The subject takes as input zero or more temporal streams. It outputs a temporal stream by applying</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum8"> 8:</span> <span style="color: #008000;">/// a delay to all incoming CTIs and dropping any events that violate the CTI. The delay represents the</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum9"> 9:</span> <span style="color: #008000;">/// maximum expected divergence between the most advanced and least advanced input stream.</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum10"> 10:</span> <span style="color: #008000;">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum11"> 11:</span> <span style="color: #008000;">/// &lt;remarks&gt;</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum12"> 12:</span> <span style="color: #008000;">/// In this alternate version of the subject, state is maintained using a custom accumulator</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum13"> 13:</span> <span style="color: #008000;">/// object rather than using the Rx Scan operator.</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum14"> 14:</span> <span style="color: #008000;">/// &lt;/remarks&gt;</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum15"> 15:</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">class</span> CtiSynchronizingSubjectAlternate</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum16"> 16:</span> {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum17"> 17:</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> ISubject&lt;PointEvent&lt;T&gt;, PointEvent&lt;T&gt;&gt; CreatePoint&lt;T&gt;(TimeSpan delay)</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum18"> 18:</span>     {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum19"> 19:</span>         <span style="color: #0000ff;">return</span> Create&lt;PointEvent&lt;T&gt;, T&gt;(delay, p =&gt; p.StartTime, t =&gt; PointEvent&lt;T&gt;.CreateCti(t));</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum20"> 20:</span>     }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum21"> 21:</span>  </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum22"> 22:</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> ISubject&lt;IntervalEvent&lt;T&gt;, IntervalEvent&lt;T&gt;&gt; CreateInterval&lt;T&gt;(TimeSpan delay)</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum23"> 23:</span>     {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum24"> 24:</span>         <span style="color: #0000ff;">return</span> Create&lt;IntervalEvent&lt;T&gt;, T&gt;(delay, i =&gt; i.StartTime, t =&gt; IntervalEvent&lt;T&gt;.CreateCti(t));</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum25"> 25:</span>     }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum26"> 26:</span>  </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum27"> 27:</span>     <span style="color: #008000;">// Edge events are not currently implemented. Care is needed to avoid unmatched start edges in the output</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum28"> 28:</span>     <span style="color: #008000;">// stream.</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum29"> 29:</span>  </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum30"> 30:</span>     <span style="color: #0000ff;">static</span> ISubject&lt;TEvent, TEvent&gt; Create&lt;TEvent, TPayload&gt;(TimeSpan delay, Func&lt;TEvent, DateTimeOffset&gt; getApplicationTime, Func&lt;DateTimeOffset, TEvent&gt; createCti)</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum31"> 31:</span>         <span style="color: #0000ff;">where</span> TEvent : TypedEvent&lt;TPayload&gt;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum32"> 32:</span>     {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum33"> 33:</span>         <span style="color: #0000ff;">if</span> (delay &lt; TimeSpan.Zero) { <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> ArgumentOutOfRangeException(<span style="color: #006080;">"delay"</span>, <span style="color: #006080;">"Delay must be non-negative."</span>); }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum34"> 34:</span>  </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum35"> 35:</span>         var accum = <span style="color: #0000ff;">new</span> Accumulator&lt;TEvent, TPayload&gt;(delay, getApplicationTime, createCti);</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum36"> 36:</span>  </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum37"> 37:</span>         <span style="color: #0000ff;">return</span> TransformSubject.Create((IObservable&lt;TEvent&gt; input) =&gt;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum38"> 38:</span>             input.Synchronize().Select(accum.Process).Where(e =&gt; e != <span style="color: #0000ff;">null</span>));</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum39"> 39:</span>     }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum40"> 40:</span>  </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum41"> 41:</span>     <span style="color: #0000ff;">sealed</span> <span style="color: #0000ff;">class</span> Accumulator&lt;TEvent, TPayload&gt;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum42"> 42:</span>         <span style="color: #0000ff;">where</span> TEvent : TypedEvent&lt;TPayload&gt;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum43"> 43:</span>     {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum44"> 44:</span>         <span style="color: #0000ff;">readonly</span> TimeSpan delay;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum45"> 45:</span>         <span style="color: #0000ff;">readonly</span> Func&lt;TEvent, DateTimeOffset&gt; getApplicationTime; </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum46"> 46:</span>         <span style="color: #0000ff;">readonly</span> Func&lt;DateTimeOffset, TEvent&gt; createCti;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum47"> 47:</span>  </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum48"> 48:</span>         DateTimeOffset? highCti;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum49"> 49:</span>  </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum50"> 50:</span>         <span style="color: #0000ff;">public</span> Accumulator(TimeSpan delay, Func&lt;TEvent, DateTimeOffset&gt; getApplicationTime, Func&lt;DateTimeOffset, TEvent&gt; createCti)</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum51"> 51:</span>         {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum52"> 52:</span>             <span style="color: #0000ff;">this</span>.delay = delay;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum53"> 53:</span>             <span style="color: #0000ff;">this</span>.getApplicationTime = getApplicationTime;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum54"> 54:</span>             <span style="color: #0000ff;">this</span>.createCti = createCti;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum55"> 55:</span>         }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum56"> 56:</span>  </pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum57"> 57:</span>         <span style="color: #0000ff;">public</span> TEvent Process(TEvent evt)</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum58"> 58:</span>         {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum59"> 59:</span>             <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">null</span> != evt)</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum60"> 60:</span>             {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum61"> 61:</span>                 var appTime = <span style="color: #0000ff;">this</span>.getApplicationTime(evt);</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum62"> 62:</span>                 <span style="color: #0000ff;">if</span> (evt.EventKind == EventKind.Cti)</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum63"> 63:</span>                 {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum64"> 64:</span>                     DateTimeOffset delayedStartTime = appTime.UtcTicks &gt; delay.Ticks ? appTime - delay : DateTimeOffset.MinValue;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum65"> 65:</span>                     <span style="color: #0000ff;">if</span> (!highCti.HasValue || delayedStartTime &gt; highCti.Value)</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum66"> 66:</span>                     {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum67"> 67:</span>                         highCti = delayedStartTime;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum68"> 68:</span>                         <span style="color: #0000ff;">return</span> createCti(delayedStartTime);</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum69"> 69:</span>                     }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum70"> 70:</span>                     <span style="color: #0000ff;">else</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum71"> 71:</span>                     {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum72"> 72:</span>                         <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">null</span>;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum73"> 73:</span>                     }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum74"> 74:</span>                 }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum75"> 75:</span>                 <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (highCti.HasValue &amp;&amp; highCti &gt; appTime)</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum76"> 76:</span>                 {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum77"> 77:</span>                     <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">null</span>;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum78"> 78:</span>                 }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum79"> 79:</span>                 <span style="color: #0000ff;">else</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum80"> 80:</span>                 {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum81"> 81:</span>                     <span style="color: #0000ff;">return</span> evt;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum82"> 82:</span>                 }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum83"> 83:</span>             }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum84"> 84:</span>             <span style="color: #0000ff;">else</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum85"> 85:</span>             {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum86"> 86:</span>                 <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">null</span>;</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum87"> 87:</span>             }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum88"> 88:</span>         }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: #606060;" id="lnum89"> 89:</span>     }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: 'Courier New', courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span style="color: #606060;" id="lnum90"> 90:</span> }</pre>
<!--CRLF--></div>
</div>]]></content:encoded>
    </item>
    <item>
      <title>StreamInsight Checkpoints: What, How and Why?</title>
      <link>https://docs.microsoft.com/archive/blogs/meek/streaminsight-checkpoints-what-how-and-why</link>
      <pubDate>Mon, 09 Apr 2012 07:39:00 GMT</pubDate>
      <dc:creator><![CDATA[Colin Meek]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/meek/2012/04/09/streaminsight-checkpoints-what-how-and-why/</guid>
      <description><![CDATA[I&rsquo;ve been fielding some questions this week on checkpoints in StreamInsight. I&rsquo;ll share...]]></description>
      <content:encoded><![CDATA[<p>I&rsquo;ve been fielding some questions this week on checkpoints in StreamInsight. I&rsquo;ll share my way of thinking about checkpoints in the hopes that it will help others build applications leveraging the feature. First, I&rsquo;ll define what a StreamInsight checkpoint represents. Then I&rsquo;ll explain how to interpret the high-water mark information StreamInsight provides to input and output adapters when a query is resumed after StreamInsight downtime (planned or unplanned). There&rsquo;s an important piece of trivia here that may surprise even veteran users!</p>
<p>The MSDN documentation outlines <a href="http://msdn.microsoft.com/en-us/library/hh290501.aspx#levels">three levels of resiliency</a>. In this post I will focus on the strictest resiliency level, but the concepts outlined are relevant for all three.</p>
<p>Think of a StreamInsight query as a black box. It takes a one or more sequences of events as inputs. It produces a sequence of events as output (in the upcoming 2.1 StreamInsight release, multiple output sequences are supported as well) <sup>*</sup>. It probably comes as no surprise that the black box contains some state. If your query is computing averages over windows, incoming events will contribute to some number of windows. Until the average over a particular window can be committed to the output, sums and counts need to be maintained internally. When you checkpoint a query, you&rsquo;re really just saving the internal state of the query. It&rsquo;s that simple!</p>
<p>Well, almost. In addition to the query state, a checkpoint captures the position of input and output sequences as of the checkpoint<sup>**</sup>. For example, if a checkpoint could speak it might say &ldquo;between enqueuing input events <i>x</i><sub>2</sub> and <i>x</i>&shy;<sub>3</sub>, and between dequeing output events <i>y</i><sub>2</sub> and <i>y</i><sub>3</sub>, this was the state of the query&rdquo;.</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/00/98/metablogapi/8422.image_58DA9E87.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-00-98-metablogapi/8422.image_5F00_58DA9E87.png"><img width="244" height="89" title="image" style="display: inline; background-image: none;" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/00/98/metablogapi/5873.image_thumb_4691D7C5.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-00-98-metablogapi/5873.image_5F00_thumb_5F00_4691D7C5.png" border="0" /></a></p>
<p>After a StreamInsight server instance has been stopped, a query can be resumed using the checkpoint state. Input and output adapters need to do some work as well. After the checkpoint was taken (but before the server instance was stopped), the input sequence may have progressed (let&rsquo;s say <i>x</i><sub>3</sub> was enqueued) and the output sequence may have progressed as well (let&rsquo;s say <i>y</i><sub>3</sub> was dequeued). Ideally, the input adapter would then replay <i>x</i><sub>3</sub> and subsequent events. And the ideal output adapter would forget that it had ever seen <i>y</i><sub>3</sub> or anything after it.</p>
<p>Instead of forgetting &ndash; by, say, deleting rows from a table or removing lines from a log file &ndash; the output adapter may instead choose to suppress output events it knows have been emitted already. The latter approach is relatively difficult to get right however:</p>
<ol>
<li>The output may include multiple events with the same timestamp(s) and payload value. Consider the case where the checkpoint is taken between two identical events with same timestamp. A na&iuml;ve de-duplication implementation may incorrectly suppress the second event because the first (equivalent) event has been emitted already.</li>
<li>While StreamInsight deterministically produces logically consistent results, there is no guarantee about the specific sequence of output events between runs. The number of CTIs may vary. The order of events with the same timestamp may also vary between runs (or even the order of events with different timestamps when using <a href="http://msdn.microsoft.com/en-us/library/microsoft.complexeventprocessing.streameventorder.aspx">StreamEventOrder.ChainOrdered</a>).</li>
<li>The output adapter needs to reliably determine which events have already been emitted.</li>
</ol>
<p>In any case, the rule of thumb when resuming a query:</p>
<p><i>Input adapters must replay events after the checkpoint; output adapters must forget events after the checkpoint.</i></p>
<p>StreamInsight provides a high-water mark (HWM, pronounced huh-wim I think) value to input adapters that can be used to determine where in the input sequence a checkpoint occurred (see <a href="http://msdn.microsoft.com/en-us/library/hh290481.aspx">IHighWaterMarkInputAdapterFactory</a> and <a href="http://msdn.microsoft.com/en-us/library/hh290530.aspx">IHighWaterMarkTypedInputAdapterFactory</a>). Output adapters get both an HWM value and an offset (see <a href="http://msdn.microsoft.com/en-us/library/hh290473.aspx">IHighWaterMarkOutputAdapterFactory</a> and <a href="http://msdn.microsoft.com/en-us/library/hh307938.aspx">IHighWaterMarkTypedOutputAdapterFactory</a>). How can these values be used to identify an element of a sequence? First, let&rsquo;s define HWM. An event <i>x<sub>i</sub></i> has high-water mark value <i>h</i> if its timestamp is <i>h</i> and for all <i>j</i> &lt; <i>i</i>, <i>x<sub>j</sub></i> has a lower timestamp. Whenever an event in a sequence has a higher timestamp than any preceding event, it has a HWM value. Assuming that your input sequence is conveniently described by an <b>IEnumerable&lt;PointEvent&lt;T&gt;&gt;</b> sequence <i>xs</i>, you can find the event corresponding to an <i>hwm</i> as follows:</p>
<p><span style="font-family: Consolas;" face="Consolas">var inputCheckpointEvent = xs.FirstOrDefault(x =&gt; x.StartTime == hwm)</span></p>
<p>Assuming that the output from the query is captured in a (resilient and persistent) <b>List&lt;PointEvent&lt;T&gt;&gt;</b>, and given <i>hwm</i> and <i>offset </i>values for an output sequence, you just need to pop ahead <i>offset</i> positions to find the checkpoint event:</p>
<p><span style="font-family: Consolas;" face="Consolas">var outputCheckpointEvent = ys <br />&nbsp;&nbsp;&nbsp; .Where(x =&gt; x.StartTime &gt;= hwm) <br />&nbsp;&nbsp;&nbsp; .Skip(offset).FirstOrDefault()</span></p>
<p>I promised a piece of trivia. While I&rsquo;ve shown you how to find the &ldquo;checkpoint event&rdquo;, I haven&rsquo;t told you where the checkpoint occurred: did it occur before the checkpoint event, or after? It turns out that the answer is <i>before</i> for an input checkpoint event but <i>after </i>for an output checkpoint event. Returning to the earlier example, if the checkpoint occurred between input events <i>x</i><sub>2</sub> and <i>x</i><sub>3</sub>, the checkpoint event will be <i>x</i><sub>3</sub>, but if the checkpoint occurred between output events <i>y</i><sub>2</sub> and <i>y</i><sub>3</sub>, the checkpoint event will be <i>y</i><sub>2</sub>! Again using the simple <b>IEnumerable&lt;&gt;</b> contract, I can demonstrate how replay works:</p>
<p><span style="font-family: Consolas;" face="Consolas">// replay all events after and <i>including</i> the checkpoint event <br />var replayXs = hwm.HasValue <br />&nbsp;&nbsp;&nbsp; ? xs.</span><a href="http://msdn.microsoft.com/en-us/library/bb549075.aspx"><span style="font-family: Consolas;" face="Consolas">SkipWhile</span></a><span style="font-family: Consolas;" face="Consolas">(x =&gt; x.StartTime &lt; hwm.Value) <br />&nbsp;&nbsp;&nbsp; : xs;</span></p>
<p>When forgetting output events and again assuming (somewhat unrealistically) that a <b>List&lt;&gt; </b>is the external reliable authority on which events have been emitted:</p>
<p><span style="font-family: Consolas;" face="Consolas">// remove all outputs events after but <span style="background-color: #ffff00;"><i>not</i> <i>including</i></span> the checkpoint event <br />if (hwm.HasValue) <br />{ <br />&nbsp;&nbsp;&nbsp; int hwmEvent = ys <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Select((y, i) =&gt; new { y.StartTime, Index = i }) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .FirstOrDefault(y =&gt; y.StartTime == hwm.Value); <br />&nbsp;&nbsp;&nbsp; if (hwmEvent != null) <br />&nbsp;&nbsp;&nbsp; { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int forgetIndex = hwmEvent.Index + offset <span style="background-color: #ffff00;">+ 1</span>; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (forgetIndex &lt; ys.Count) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ys.RemoveRange(forgetIndex, ys.Count &ndash; forgetIndex); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <br />&nbsp;&nbsp;&nbsp; } <br />}</span></p>
<p>Let&rsquo;s tie all of this information together with a simple walkthrough. In this example, the query state is described by the input events contributing to the state.</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="148" valign="top">
<p><b>Action</b></p>
</td>
<td width="96" valign="top">
<p><b>Query state</b></p>
</td>
<td width="101" valign="top">
<p><b>Output list</b></p>
</td>
<td width="224" valign="top">
<p><b>Notes</b></p>
</td>
</tr>
<tr>
<td width="148" valign="top">
<p>Enqueue <i>x</i><sub>1</sub></p>
</td>
<td width="96" valign="top">
<p>{ <i>x</i><sub>1</sub> }</p>
</td>
<td width="101" valign="top">
<p>{ <i>y</i><sub>1</sub> }</p>
</td>
<td width="224" valign="top">&nbsp;</td>
</tr>
<tr>
<td width="148" valign="top">
<p>Begin checkpoint</p>
</td>
<td width="96" valign="top">
<p>{ <i>x</i><sub>1</sub> }</p>
</td>
<td width="101" valign="top">
<p>{ <i>y</i><sub>1</sub> }</p>
</td>
<td width="224" valign="top">
<p>Waiting for HWM event on input</p>
</td>
</tr>
<tr>
<td width="148" valign="top">
<p>Enqueue <i>x</i><sub>2</sub></p>
</td>
<td width="96" valign="top">
<p>{ <i>x</i><sub>1</sub>, <i>x</i><sub>2</sub> }</p>
</td>
<td width="101" valign="top">
<p>{ <i>y</i><sub>1</sub>, <i>y</i><sub>2</sub> }</p>
</td>
<td width="224" valign="top">
<p>Let&rsquo;s assume <i>x</i><sub>2</sub> does not increase the HWM, so the checkpoint cannot be initiated yet</p>
</td>
</tr>
<tr>
<td width="148" valign="top">
<p>Enqueue <span style="background-color: #ffff00;"><i>x</i><sub>3</sub></span></p>
</td>
<td width="96" valign="top">
<p><span style="background-color: #ffc000;">{ <i>x</i><sub>1</sub>, <i>x</i><sub>2</sub> }</span></p>
</td>
<td width="101" valign="top">
<p>{ <i>y</i><sub>1</sub>, <i>y</i><sub>2</sub> }</p>
</td>
<td width="224" valign="top">
<p><span style="background-color: #ffff00;"><i>x</i><sub>3</sub></span> is the input checkpoint event, current query <span style="background-color: #ffc000;">state</span> is captured <i>before</i> processing <span style="background-color: #ffff00;"><i>x</i><sub>3</sub></span></p>
</td>
</tr>
<tr>
<td width="148" valign="top">
<p>(continued)</p>
</td>
<td width="96" valign="top">
<p>{ <i>x</i><sub>1</sub>, <i>x</i><sub>2</sub>, <i>x</i><sub>3</sub> }</p>
</td>
<td width="101" valign="top">
<p>{ <i>y</i><sub>1</sub>, <span style="background-color: #00ff00;"><i>y</i><sub>2</sub></span>, <i>y</i><sub>3 </sub>}</p>
</td>
<td width="224" valign="top">
<p><span style="background-color: #00ff00;"><i>y</i><sub>2</sub></span> is the output checkpoint event, as it is the last event before the checkpoint</p>
</td>
</tr>
<tr>
<td width="148" valign="top">
<p>End checkpoint</p>
</td>
<td width="96" valign="top">
<p>{ <i>x</i><sub>1</sub>, <i>x</i><sub>2</sub>, <i>x</i><sub>3</sub> }</p>
</td>
<td width="101" valign="top">
<p>{ <i>y</i><sub>1</sub>, <i>y</i><sub>2</sub>, <i>y</i><sub>3 </sub>}</p>
</td>
<td width="224" valign="top">
<p>Checkpoint &lt;<span style="background-color: #ffc000;">{ <i>x</i><sub>1</sub>, <i>x</i><sub>2</sub> }</span>, <span style="background-color: #ffff00;"><i>x</i><sub>3</sub></span>, <span style="background-color: #00ff00;"><i>y</i><sub>2</sub></span>&gt; committed to disk</p>
</td>
</tr>
<tr>
<td width="148" valign="top">
<p>Enqueue <i>x</i><sub>4</sub></p>
</td>
<td width="96" valign="top">
<p>{ <i>x</i><sub>1</sub>, <i>x</i><sub>2</sub>, <i>x</i><sub>3</sub>, <i>x</i><sub>4</sub> }</p>
</td>
<td width="101" valign="top">
<p>{ <i>y</i><sub>1</sub>, <i>y</i><sub>2</sub>, <i>y</i><sub>3</sub>, <i>y</i><sub>4</sub> }</p>
</td>
<td width="224" valign="top">&nbsp;</td>
</tr>
<tr>
<td width="148" valign="top">
<p>Downtime!</p>
</td>
<td width="96" valign="top">&nbsp;</td>
<td width="101" valign="top">&nbsp;</td>
<td width="224" valign="top">&nbsp;</td>
</tr>
<tr>
<td width="148" valign="top">
<p>Resume</p>
</td>
<td width="96" valign="top">
<p><span style="background-color: #ffc000;">{ <i>x</i><sub>1</sub>, <i>x</i><sub>2</sub> }</span></p>
</td>
<td width="101" valign="top">
<p>{ <i>y</i><sub>1</sub>, <span style="background-color: #00ff00;"><i>y</i><sub>2</sub></span>, <i><s>y</s></i><s><sub>3</sub>, <i>y</i><sub>4</sub></s> }</p>
</td>
<td width="224" valign="top">
<p>StreamInsight recovers query <span style="background-color: #ffc000;">state</span>, output adapter forgets all output events after <span style="background-color: #00ff00;"><i>y</i><sub>2</sub></span></p>
</td>
</tr>
<tr>
<td width="148" valign="top">
<p>Enqueue <span style="background-color: #ffff00;"><i>x</i><sub>3</sub></span></p>
</td>
<td width="96" valign="top">
<p>{ <i>x</i><sub>1</sub>, <i>x</i><sub>2</sub>, <i>x</i><sub>3</sub> }</p>
</td>
<td width="101" valign="top">
<p>{ <i>y</i><sub>1</sub>, <i>y</i><sub>2</sub>, <i>y</i><sub>3 </sub>}</p>
</td>
<td width="224" valign="top">
<p>Input replays from <span style="background-color: #ffff00;"><i>x</i><sub>3</sub></span></p>
</td>
</tr>
<tr>
<td width="148" valign="top">
<p>Enqueue <i>x</i><sub>4</sub></p>
</td>
<td width="96" valign="top">
<p>{ <i>x</i><sub>1</sub>, <i>x</i><sub>2</sub>, <i>x</i><sub>3</sub>, <i>x</i><sub>4</sub> }</p>
</td>
<td width="101" valign="top">
<p>{ <i>y</i><sub>1</sub>, <i>y</i><sub>2</sub>, <i>y</i><sub>3</sub>, <i>y</i><sub>4</sub> }</p>
</td>
<td width="224" valign="top">&nbsp;</td>
</tr>
<tr>
<td width="148" valign="top">
<p>&hellip;</p>
</td>
<td width="96" valign="top">&nbsp;</td>
<td width="101" valign="top">&nbsp;</td>
<td width="224" valign="top">
<blockquote style="margin-right: 0px;" dir="ltr">
<p>&nbsp;</p>
</blockquote>
</td>
</tr>
</tbody>
</table>
<p>Notice that by replaying input events and forgetting output events, we see correct output after resuming the query!</p>
<p>* Notice that I&rsquo;m talking about &ldquo;sequences of events&rdquo; rather than &ldquo;temporal streams&rdquo;. While StreamInsight operators are defined in terms of temporal streams &ndash; sequences of timestamped events with ordering constraints imposed by common time increments (or CTIs) &ndash; a checkpoint can be understood relative to generic sequences.</p>
<p>** StreamInsight doesn&rsquo;t actually freeze the query operators, inputs and outputs in order to take a snapshot of the internal state, but this is a useful and accurate way of characterizing the logical contract for checkpoints. Details of the internal mechanism aren&rsquo;t covered in this blog post.</p>]]></content:encoded>
    </item>
    <item>
      <title>Sequences and StreamInsight: Initiating a Computation</title>
      <link>https://docs.microsoft.com/archive/blogs/meek/sequences-and-streaminsight-initiating-a-computation</link>
      <pubDate>Wed, 21 Mar 2012 18:25:00 GMT</pubDate>
      <dc:creator><![CDATA[Colin Meek]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/meek/2012/03/21/sequences-and-streaminsight-initiating-a-computation/</guid>
      <description><![CDATA[By Colin Meek, Alex Raizman, and Rafael Fernandez Moctezuma
When bridging between temporal streams...]]></description>
      <content:encoded><![CDATA[<p><i>By Colin Meek, Alex Raizman, and Rafael Fernandez Moctezuma</i></p>
<p>When bridging between temporal streams (<b>CepStream</b>), pull-sequences (<b>IEnumerable</b>), and push sequences (<b>IObservable</b>) using the StreamInsight <a href="http://blogs.msdn.com/b/meek/archive/2010/10/27/streaminsight-sequence-integration-five-easy-pieces.aspx">sequence integration feature</a>, handling of computation lifetime is automatic: consuming results from a query generally kicks off upstream queries*, and disposing or deleting releases all upstream resources as well. While most of the time the policies governing lifetimes &ldquo;just work&rdquo;, the resulting behaviors sometimes confuse StreamInsight developers. Consider the following example:</p>
<p style="padding-left: 30px;"><span style="font-family: 'courier new', courier;">var source = Enumerable.Range(0, 1024);</span><br /><span style="font-family: 'courier new', courier;">int count = 0;</span><br /><span style="font-family: 'courier new', courier;">using (source.ToObservable().Subscribe(_ =&gt; count++))</span><br /><span style="font-family: 'courier new', courier;">{</span><br /><span style="font-family: 'courier new', courier;">&nbsp; &nbsp; Console.WriteLine(count); // 1024</span><br /><span style="font-family: 'courier new', courier;">}&nbsp;</span></p>
<p style="padding-left: 30px;"><span style="font-family: 'courier new', courier;"><b>Example 1: Pull-to-push adapter</b></span></p>
<p>The call to <b>Subscribe()</b> in this example returns after all elements of the source sequence have been consumed.&nbsp;</p>
<p>Consider the following example that bridges from a push-sequence to a temporal stream:</p>
<p style="padding-left: 30px;"><span style="font-family: 'courier new', courier;">int count = 0;</span><br /><span style="font-family: 'courier new', courier;">var source = Observable.Range(0, 1024).Do(_ =&gt; count++);</span><br /><span style="font-family: 'courier new', courier;">var startTime = new DateTimeOffset(new DateTime(2010, 1, 1), TimeSpan.Zero);</span><br /><span style="font-family: 'courier new', courier;">var stream = source.ToPointStream(Application, i =&gt; PointEvent.CreateInsert(startTime.AddMinutes(i), i));</span><br /><span style="font-family: 'courier new', courier;">var query = stream.ToQuery("MyQuery", null, EventShape.Point, StreamEventOrder.FullyOrdered);</span><br /><span style="font-family: 'courier new', courier;">query.Start();</span><br /><span style="font-family: 'courier new', courier;">Console.WriteLine(count); // 1024</span></p>
<p style="padding-left: 30px;"><span style="font-family: 'courier new', courier;"><b>Example 2: Observable range generator and push-to-stream adapter</b></span></p>
<p>Similar to the previous example, you&rsquo;ll notice that the call to <b>query.Start()</b> does not return until all of the input has been consumed.</p>
<p>The following calls are used to initiate a computation in StreamInsight: <b>Query.Start()</b>, <b>IEnumerable&lt;&gt;.GetEnumerator()</b>, and <b>IObservable&lt;&gt;.Subscribe()</b>. As the examples 1 and 2 suggest, we&rsquo;re particularly interested in <i>when these calls return</i>. First, we need to understand that the Reactive Framework embraces the &ldquo;least concurrency&rdquo; principle in its design, which can be roughly summarized as &ldquo;don&rsquo;t introduce concurrency if you don&rsquo;t need to&rdquo;. The MSDN article on &ldquo;<a href="http://msdn.microsoft.com/en-us/library/hh242963(v=VS.103).aspx">Using Schedulers</a>&rdquo; gives additional background; you may also consult the <a href="https://channel9.msdn.com/Series/Rx-Workshop/Rx-Workshop-Schedulers">Rx Workshop: Schedulers</a> on Channel 9 for an in-depth look.</p>
<p>What does this principle imply about the above examples?</p>
<p>Let&rsquo;s first examine the <b>ToObservable()</b> method. By default, it uses the <a href="http://msdn.microsoft.com/en-us/library/system.reactive.concurrency.currentthreadscheduler(v=vs.103).aspx">current thread scheduler</a>&nbsp;so the work of consuming the source iterator happens on the&nbsp;&ldquo;subscribe&rdquo; thread! By the time the subscribe call returns, all 1024 source elements have already been processed.</p>
<p>What is the effect of the temporal query sandwiched between the observable source and published stream output in example 2?</p>
<p>When&nbsp;<b>query.Start()&nbsp;</b>or another &lsquo;initiating&rsquo; method like&nbsp;<b>GetEnumerator()&nbsp;</b>or&nbsp;<b>Subscribe()&nbsp;</b>is called,&nbsp;<i>the method doesn&rsquo;t return until all inputs to the query have been initiated</i>. This behavior allows the caller to know when a temporal query is primed to accept input. It also means that -- depending on the nature of the sources feeding your query -- you may need to wait a long time for the call to return!</p>
<p>How can you prevent generators like <b>Observable.Range()</b>&nbsp;and <strong>Observable.</strong><b>ToObservable() </b>from blocking initiation requests?</p>
<p>We can manually introduce the desired concurrency by passing in a different scheduler! For instance, we could modify example 1 so&nbsp;that events are enqueued on thread pool threads rather than the current thread:</p>
<p style="padding-left: 30px;"><span style="font-family: 'courier new', courier;">using (source.ToObservable(<span style="background-color: #ffff00;">Scheduler.ThreadPool</span>).Subscribe(_ =&gt; count++))</span><br /><span style="font-family: 'courier new', courier;">{</span><br /><span style="font-family: 'courier new', courier;">&nbsp; &nbsp; Console.WriteLine(count); // ~0</span><br /><span style="font-family: 'courier new', courier;">}</span></p>
<p style="padding-left: 30px;"><span style="font-family: 'courier new', courier;"><b>Example 3: Introducing concurrency using a non-default scheduler</b></span><b></b></p>
<p>Let&rsquo;s look at an example where the call to <b>Subscribe()&nbsp;</b>can never return because the source &ldquo;<b><a href="http://msdn.microsoft.com/en-us/library/ff402861(v=VS.92).aspx">Never</a>()</b>&rdquo; calls <b>OnCompleted</b> or <b>OnError</b>:</p>
<p style="padding-left: 30px;"><span style="font-family: 'courier new', courier;">using (Observable.Never&lt;int&gt;().ToEnumerable().ToObservable().Subscribe())</span><br /><span style="font-family: 'courier new', courier;">{</span><br /><span style="font-family: 'courier new', courier;">&nbsp; &nbsp; Console.WriteLine("Inconceivable!");</span><br /><span style="font-family: 'courier new', courier;">}</span></p>
<p style="padding-left: 30px;"><span style="font-family: 'courier new', courier;"><b>Example 4: Push-to-pull-to-push</b></span></p>
<p>Fortunately, the above example isn&rsquo;t terribly interesting! Just keep in mind that <b>xs.ToEnumerable().ToObservable()</b> is <i>not</i> equivalent to <b>xs</b>:</p>
<p style="padding-left: 30px;"><span style="font-family: 'courier new', courier;">using (Observable.Never&lt;int&gt;().Subscribe())</span><br /><span style="font-family: 'courier new', courier;">{</span><br /><span style="font-family: 'courier new', courier;">&nbsp; &nbsp; Console.WriteLine("Conceivable!");</span><br /><span style="font-family: 'courier new', courier;">}</span>&nbsp;</p>
<p style="padding-left: 30px;"><span style="font-family: 'courier new', courier;"><b>Example 5: Absent the push-pull-push bridges, subscribe no longer blocks</b></span></p>
<p>Apart from modifying the behavior of <b>Subscribe()</b>,the intervening buffer used by <b>ToEnumerable()</b> also makes calls to <b>On*</b> asynchronous.</p>
<p>In the pull-sequence space, propagation of initiation requests looks a little bit different. Because LINQ to Objects mostly follows <b>yield return</b> semantics, calls to <b>GetEnumerator()</b> do not in fact propagate immediately! It is not until the first call to <b>MoveNext() </b>on the iterator object that the compiler-generated iterator state machine advances beyond the <b>GetEnumerator() </b>calls for inputs. This behavior contrasts with those of Reactive and StreamInsight operators whose inputs are (generally) spun up immediately. There&rsquo;s even an exception to the exception: for LINQ to SQL and LINQ to Entities queries, calls to <b>GetEnumerator()</b> immediately result in <b>ExecuteReader()</b> calls. Our hope is that the exact phase of the initiation is not as important for pull-based sources as it is for potentially hot push-based sources. In the latter case, it may be important to ensure that a downstream consumer is active before the upstream producer begins generating output**.</p>
<p>&nbsp;Let me know if you&rsquo;d like to see a follow-up post on end-of-life concerns for computations in StreamInsight (auto-detach, synchronization, etc.)!</p>
<p>* Initiation requests do <i>not</i> propagate automatically across published stream and subject boundaries.<br />** Hot and cold, see Wes&rsquo; talk on Channel 9: <a href="https://channel9.msdn.com/blogs/j.van.gogh/rx-api-in-depth-hot-and-cold-observables">Rx API in depth: Hot and Cold observables</a>.</p>]]></content:encoded>
    </item>
    <item>
      <title>LINQ “Macros” in StreamInsight 1.2: Left Outer Join</title>
      <link>https://docs.microsoft.com/archive/blogs/meek/linq-macros-in-streaminsight-1-2-left-outer-join</link>
      <pubDate>Mon, 25 Jul 2011 10:56:03 GMT</pubDate>
      <dc:creator><![CDATA[Colin Meek]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/meek/2011/07/25/linq-macros-in-streaminsight-1-2-left-outer-join/</guid>
      <description><![CDATA[In an earlier post, I discussed implementation of custom query operators that combine existing...]]></description>
      <content:encoded><![CDATA[<p>In an <a href="http://blogs.msdn.com/b/meek/archive/2011/01/05/linq-macros.aspx">earlier post</a>, I discussed implementation of custom query operators that combine existing built-in operators. In <a href="http://www.microsoft.com/download/en/details.aspx?id=26720">StreamInsight 1.2</a>, we have made some changes to simplify implementation of custom operators. In the running example from the previous post, I showed how you can manually construct an expression tree representing a Left Anti Semi Join (LASJ), which required a fair amount of code in StreamInsight 1.1:</p>  <blockquote>   <pre>public static CepStream&lt;TLeft&gt; LeftAntiSemiJoin&lt;TLeft, TRight&gt;(<br />&#160;&#160;&#160; this CepStream&lt;TLeft&gt; left, <br />&#160;&#160;&#160; CepStream&lt;TRight&gt; right,<br />&#160;&#160;&#160; Expression&lt;Func&lt;TLeft, TRight, bool&gt;&gt; predicate)<br />{<br />    …<br /> <br />&#160;&#160;&#160; // l =&gt; right.Where(r =&gt; predicate(l, r)).IsEmpty()<br />&#160;&#160;&#160; var leftPredicate = Expression.Lambda&lt;Func&lt;TLeft, bool&gt;&gt;(<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; Expression.Call(isEmptyMethod,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Expression.Call(whereMethod,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Expression.Constant(right),<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Expression.Lambda&lt;Func&lt;TRight, bool&gt;&gt;(predicate.Body, rightPrm))),<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; leftPrm);<br /> <br />&#160;&#160;&#160; return left.Where(leftPredicate);<br />}</pre>

  <p><b>Code fragment 1: LASJ in StreamInsight 1.1</b></p>
</blockquote>

<p>In StreamInsight 1.2, we have added support for invocation expressions. Take a look at Joe and Ben Albahari’s article on <a href="http://www.albahari.com/nutshell/predicatebuilder.aspx">Dynamically Composing Expression Predicates</a> for an idea of how to use this feature. Code fragment 1 can be rewritten as:</p>

<blockquote>
  <pre>public static CepStream&lt;TLeft&gt; LeftAndSemiJoin&lt;TLeft, TRight&gt;(<br />&#160;&#160;&#160; this CepStream&lt;TLeft&gt; left, <br />&#160;&#160;&#160; CepStream&lt;TRight&gt; right, <br />&#160;&#160;&#160; Expression&lt;Func&lt;TLeft, TRight, bool&gt;&gt; predicate)<br />{<br />&#160;&#160;&#160; return from l in left<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; where (from r in right<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; where predicate.Compile()(l, r)<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; select r).IsEmpty()<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; select l;<br />}</pre>

  <p><b>Code fragment 2: LASJ in StreamInsight 1.2</b></p>
</blockquote>

<p>The invocation expression has been highlighted. Notice that we <a href="http://msdn.microsoft.com/en-us/library/bb345362.aspx">Compile</a> the predicate expression. What does this call represent in a StreamInsight query? StreamInsight recognizes the Compile method but instead of compiling to a CLR delegate – an opaque delegate that cannot be evaluated remotely – it “compiles” a StreamInsight query in which the predicate expression has been inlined.</p>

<p>Now let’s take our LASJ operator and use it to construct a <a href="http://en.wikipedia.org/wiki/Join_(SQL)#Left_outer_join">Left Outer Join</a> (LOJ) operator. For most LINQ providers, there are several ways of describing <a href="http://msdn.microsoft.com/en-us/library/bb397895.aspx">LOJ</a>, usually involving the <a href="http://msdn.microsoft.com/en-us/library/bb355419.aspx">DefaultIfEmpty</a> operator. StreamInsight does not have a DefaultIfEmpty operator. Asking if an infinite stream is empty is potentially dangerous* so we’ll try something different. LOJ can also be written as the union of an inner join and LASJ, as in the following example:</p>

<blockquote>
  <pre>public static CepStream&lt;TResult&gt; LeftOuterJoin&lt;TLeft, TRight, TResult&gt;(<br />&#160;&#160;&#160; this CepStream&lt;TLeft&gt; left, <br />&#160;&#160;&#160; CepStream&lt;TRight&gt; right,<br />&#160;&#160;&#160; Expression&lt;Func&lt;TLeft, TRight, bool&gt;&gt; predicate,<br />&#160;&#160;&#160; Expression&lt;Func&lt;TLeft, TResult&gt;&gt; outerSelector,<br />&#160;&#160;&#160; Expression&lt;Func&lt;TLeft, TRight, TResult&gt;&gt; innerSelector)<br />{<br />&#160;&#160;&#160; // left elements with no matching right elements:<br />&#160;&#160;&#160; var outer = from l in left.LeftAndSemiJoin(right, predicate)<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; select outerSelector.Compile().Invoke(l);<br /> <br />&#160;&#160;&#160; // left elements with matching right elements:<br />&#160;&#160;&#160; var inner = from l in left<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; from r in right<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; where predicate.Compile().Invoke(l, r)<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; select innerSelector.Compile().Invoke(l, r);<br /> <br />&#160;&#160;&#160; return outer.Union(inner);&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />}</pre>

  <p><b>Code fragment 3: LOJ in StreamInsight 1.2</b></p>
</blockquote>

<p>Notice that you can use either the delegate “Invoke” method as in the LeftOuterJoin example:</p>

<blockquote>
  <p><font face="Courier New">predicate.Compile().Invoke(l, r)</font></p>
</blockquote>

<p>or the more traditional invocation syntax used in the LeftAntiSemiJoinExample:</p>

<blockquote>
  <p><font face="Courier New">predicate.Compile()(l, r)</font></p>
</blockquote>

<p>The “selector” expressions produce identically-typed results for the case where the left-hand side has a matching event on the right-hand side (innerSelector) and the case where it does not (outerSelector), as in the following example:</p>

<blockquote>
  <pre>using (var server = Server.Create(&quot;Default&quot;))<br />{<br />&#160;&#160;&#160; var app = server.CreateApplication(&quot;loj&quot;);<br /> <br />&#160;&#160;&#160; // create input data<br />&#160;&#160;&#160; var left = new[]<br />&#160;&#160;&#160; {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; createInsert(0, 2, 1),<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; createInsert(1, 1.5, 2),<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; createInsert(3, 4, 3),<br />&#160;&#160;&#160; }.ToStream(app, AdvanceTimeSettings.IncreasingStartTime);<br /> <br />&#160;&#160;&#160; var right = new[]<br />&#160;&#160;&#160; {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; createInsert(0.5, 1.5, 1),<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; createInsert(1.25, 1.5, 2),<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; createInsert(3.5, 4.5, 3),<br />&#160;&#160;&#160; }.ToStream(app, AdvanceTimeSettings.IncreasingStartTime);<br /> <br />&#160;&#160;&#160; // left outer join<br />&#160;&#160;&#160; var query = left.LeftOuterJoin(<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; right,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; (l, r) =&gt; l == r,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; l =&gt; new { Left = l, Right = (int?)null },<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; (l, r) =&gt; new { Left = l, Right = (int?)r });<br />}</pre>

  <p><b>Code fragment 4: Using LOJ operator within a StreamInsight query.</b></p>
</blockquote>

<p>I mentioned a limitation on macro use within lambda bodies in my previous post. This limitation no longer exists: when StreamInsight encounters an unrecognized method (e.g. LeftOuterJoin in the above example) returning a stream definition, it will invoke the method to see what it returns, on the grounds that doing so may be better than failing.</p>

<p>The examples in this post are included in our <a href="http://blogs.msdn.com/b/streaminsight/archive/2011/07/19/updated-linqpad-driver-and-samples.aspx">updated LINQPad samples</a>. Give them a try!</p>

<p>* Yes, there’s something odd about StreamInsight’s <a href="http://msdn.microsoft.com/en-us/library/ff518535.aspx">IsEmpty</a> operator. It is permitted strictly in the context of the LASJ pattern where it asks not <i>whether</i> a stream is empty in its entirety, but <i>when</i> the stream is empty. Appropriate for a query language with a temporal bias!</p>]]></content:encoded>
    </item>
    <item>
      <title>LINQ Queries as Streams and Thread Safety</title>
      <link>https://docs.microsoft.com/archive/blogs/meek/linq-queries-as-streams-and-thread-safety</link>
      <pubDate>Thu, 27 Jan 2011 11:32:00 GMT</pubDate>
      <dc:creator><![CDATA[Colin Meek]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/meek/2011/01/27/linq-queries-as-streams-and-thread-safety/</guid>
      <description><![CDATA[A few weeks ago, Torsten Grabs – a colleague on the StreamInsight team and accomplished violist –...]]></description>
      <content:encoded><![CDATA[<p>A few weeks ago, Torsten Grabs – a colleague on the StreamInsight team and accomplished violist – came to me with a potential bug. He was seeing an ADO.NET SqlClient exception when running a StreamInsight query: “<em>There is already an open DataReader associated with this Command which must be closed first.</em>” I suggested he enable <a href="http://msdn.microsoft.com/en-us/library/ms345109(SQL.90).aspx">Multiple Active Result Sets</a> (MARS) in his SQL connection and this appeared to solve the problem. Digging a little deeper, a few issues occurred to us that will affect StreamInsight and <a href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx">Rx</a> queries using external data sources. Before starting to dig, let’s dissect Torsten’s code to understand what’s happening.</p>  <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:12d2fc27-2417-4c38-a676-0c81376c8c89" class="wlWriterEditableSmartContent"> <div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"> <div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="color:#0000ff">static</span> <span style="color:#0000ff">void</span> Main(<span style="color:#0000ff">string</span>[] args)<br /> {<br />     <span style="color:#0000ff">using</span> (<span style="color:#0000ff">var</span> ctx = <span style="color:#0000ff">new</span> <span style="color:#2b91af">MyObjectContext</span>())<br />     {<br />         <span style="color:#0000ff">var</span> source1 = <span style="color:#0000ff">from</span> c <span style="color:#0000ff">in</span> ctx.Table1<br />                       ...<br /> <br />         var source2 = <span style="color:#0000ff">from</span> c <span style="color:#0000ff">in</span> ctx.Table2<br />                       ...<br /> <br />         <span style="color:#0000ff">using</span> (<span style="color:#0000ff">var</span> server = <span style="color:#2b91af">Server</span>.Create(<span style="color:#a31515">&quot;Default&quot;</span>))<br />         {<br />             <span style="color:#0000ff">var</span> app = server.CreateApplication(<span style="color:#a31515">&quot;ThreadSafety&quot;</span>);<br /> <br />             <span style="color:#0000ff">var</span> stream1 = source1.ToPointStream(app, ...);<br />             <br />             <span style="color:#0000ff">var</span> stream2 = source1.ToPointStream(app, ...);<br /> <br />             <span style="color:#0000ff">var</span> query = <span style="color:#0000ff">from</span> e1 <span style="color:#0000ff">in</span> stream1<br />                         <span style="color:#0000ff">from</span> e2 <span style="color:#0000ff">in</span> stream2<br />                         ...<br /> <br />             var sink = query.ToEnumerable();<br /> <br />             <span style="color:#0000ff">using</span> (<span style="color:#0000ff">var</span> enumerator = sink.GetEnumerator()) <span style="color:#008000">// Exception!</span></div> </div> </div>  <p>(StreamInsight 1.1 adds support for .NET event sequences. Using the <strong>ToStream</strong>, <strong>ToPointStream</strong>, etc. extension methods, arbitrary LINQ queries (<strong>IEnumerable&lt;&gt;</strong>, <strong>IQueryable&lt;&gt;</strong>, <strong>IObservable&lt;&gt;</strong>, or <strong>IQbservable&lt;&gt;</strong>) can be turned into event streams processed by the StreamInsight engine. For a quick introduction to the feature, see my <a href="http://blogs.msdn.com/b/meek/archive/2010/10/27/streaminsight-sequence-integration-five-easy-pieces.aspx">previous post</a>.)</p>  <p>First of all, what’s a SqlClient exception doing in a StreamInsight query? StreamInsight doesn’t leverage the SqlClient libraries, or SQL Server for that matter. The exception originates in the LINQ to Entities query source and passes <em>through </em>the StreamInsight query. When the StreamInsight query is evaluated, it automatically triggers evaluation of the underlying LINQ to Entities queries where the problem occurs.</p>  <p>Why does this program result in multiple simultaneous data readers? The behavior of <strong>ToPointStream</strong> is the key. StreamInsight turns an <strong>IEnumerable </strong>into a stream by pulling on its iterator from a dedicated thread. In the above example, the same SQL connection is being used by two data readers simultaneously because <strong>stream1</strong> and <strong>stream2</strong> are being processed in parallel by the StreamInsight <strong>query</strong>.</p>  <p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/00/98/metablogapi/7002.image_2.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-01-00-98-metablogapi/7002.image_5F00_2.png"><img style="background-image: none; border-right-width: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/00/98/metablogapi/2046.image_thumb.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-01-00-98-metablogapi/2046.image_5F00_thumb.png" width="585" height="437" /></a></p>  <p>By enabling MARS (setting <strong>MultipleActiveResultSets=true </strong>in our SQL connection string), we can <em>partially</em> address the problem Torsten encountered. However, you need to look closely at the thread-safety boilerplate in MSDN for the Entity Framework. In summary, it isn’t. In practice, you may see intermittent failures if you attempt to access the same <strong>ObjectContext</strong> simultaneously on two threads. See the <a href="http://www.ef-faq.org/objectcontext.html">EF FAQ</a> site for more information.</p>  <p>A more reliable solution uses separate contexts for each query:</p>  <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4a5f9385-a878-4cf9-a0bd-c0a233c616e1" class="wlWriterEditableSmartContent"> <div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"> <div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="color:#0000ff">using</span> (<span style="color:#0000ff">var</span> ctx1 = <span style="color:#0000ff">new</span> <span style="color:#2b91af">MyObjectContext</span>())<br /> <span style="color:#0000ff">using</span> (<span style="color:#0000ff">var</span> ctx2 = <span style="color:#0000ff">new</span> <span style="color:#2b91af">MyObjectContext</span>())<br /> {<br />     <span style="color:#0000ff">var</span> source1 = <span style="color:#0000ff">from</span> c <span style="color:#0000ff">in</span> ctx1.Table1<br />                   ...<br /> <br />     var source2 = <span style="color:#0000ff">from</span> c <span style="color:#0000ff">in</span> ctx2.Table2<br />                   ...</div> </div> </div>  <p>Apart from thread safety concerns, some other advise applies when using LINQ to Entities or LINQ to SQL with StreamInsight:</p>  <ol>   <li>Use <a href="http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.versioncontrol.client.mergeoptions.aspx">MergeOptions.NoTracking</a> or <a href="http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.objecttrackingenabled.aspx">ObjectTrackingEnabled = false</a> if you’re merely streaming results. There is no need to track entities in the <strong>ObjectContext</strong> or <strong>DataContext</strong> if StreamInsight is the only consumer of query results. </li>    <li>StreamInsight imposes strong restrictions on the shape of event payloads. Ensure that source queries project only supported primitive fields, as outlined in “<a href="http://msdn.microsoft.com/en-us/library/ee378905(SQL.105).aspx">Payload Field Requirements</a>”. </li>    <li>To ensure that StreamInsight can efficiently process a query source as a temporal stream, consider ordering the source query by event sync time. </li> </ol>  <p>The above advise is outlined in the following more detailed code example:</p>  <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:2ebf1f4e-c08a-4971-997b-1caa92b54eb0" class="wlWriterEditableSmartContent"> <div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"> <div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap">ctx1.Table1.MergeOption = <span style="color:#2b91af">MergeOption</span>.NoTracking;<br /> <br /> <span style="color:#0000ff">var</span> source1 = <span style="color:#0000ff">from</span> c <span style="color:#0000ff">in</span> ctx1.Table1<br />               <span style="color:#0000ff">where</span> c.Created != <span style="color:#0000ff">null</span><br />               <span style="color:#008000">// order by &quot;Created&quot; time, which is the sync time</span><br />               <span style="color:#008000">// for point event inputs</span><br />               <span style="color:#0000ff">orderby</span> c.Created<br />               <span style="color:#008000">// project supported payload fields</span><br />               <span style="color:#0000ff">select</span> <span style="color:#0000ff">new</span> { Created = c.Created.Value, c.ID, c.Text };<br /> <br /> <span style="color:#0000ff">var</span> stream1 = source1.ToPointStream(app,<br />     r =&gt; <span style="color:#2b91af">PointEvent</span>.CreateInsert(r.Created, r),<br />     <span style="color:#008000">// indicate that sync times are monotonic</span><br />     <span style="color:#2b91af">AdvanceTimeSettings</span>.IncreasingStartTime);</div> </div> </div>]]></content:encoded>
    </item>
    <item>
      <title>LINQ Macros</title>
      <link>https://docs.microsoft.com/archive/blogs/meek/linq-macros</link>
      <pubDate>Wed, 05 Jan 2011 13:30:20 GMT</pubDate>
      <dc:creator><![CDATA[Colin Meek]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/meek/2011/01/05/linq-macros/</guid>
      <description><![CDATA[A colleague was asking how to construct a particular LINQ “operator macro” today. Basically, he was...]]></description>
      <content:encoded><![CDATA[<p style="line-height: 13pt; list-style-type: disc; margin: 0in 0in 10pt" class="MsoNormal"><font color="#000000" face="Calibri"><font style="font-size: 11pt"><a href="http://blogs.msdn.com/b/romans/">A colleague</a> was asking how to construct a particular LINQ “operator macro” today. Basically, he was finding it inconvenient to repeat boilerplate for particular operator patterns in his code, but was struggling to inline expression snippets into LINQ query expressions. Thought I’d share a sample macro because the pattern is generally useful. I’ll illustrate a simple </font></font><font style="font-size: 11pt"><a style="cursor: auto" href="http://msdn.microsoft.com/en-us/library/aa178588(SQL.80).aspx"><font color="#0000ff" face="Calibri"><u>Left Anti Semi Join</u></font></a></font><font face="Calibri"><font style="font-size: 11pt" color="#000000"> (LASJ) operator macro, but you can hopefully recognize other possibilities as well.</font></font></p>  <p style="line-height: 13pt; list-style-type: disc; margin: 0in 0in 10pt" class="MsoNormal"><font face="Calibri"><font style="font-size: 11pt" color="#000000">The LASJ operator takes as arguments a “left” collection, a “right” collection and a join predicate. It returns all elements on the left that have no matching elements on the right. My first attempt uses the LINQ “Any” operator:</font></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><font color="#0000ff"><font style="font-size: 9pt">public</font></font></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#000000"> </font></span><span style="font-family: ; color: ; font-size: "><font color="#0000ff">static</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000"> IEnumerable&lt;TLeft&gt; LeftAntiSemiJoin&lt;TLeft, TRight&gt;(</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9pt">&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">this</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000"> IEnumerable&lt;TLeft&gt; left, </font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: ; color: ; font-size: "><font face="Consolas"><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160; </font></span><font style="font-size: 9pt">IEnumerable&lt;TRight&gt; right, </font></font></font></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160; </font></span><font style="font-size: 9pt">Func&lt;TLeft, TRight, </font></font></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">bool</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000">&gt; predicate)</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: ; color: ; font-size: "><font face="Consolas"><font style="font-size: 9pt" color="#000000">{</font></font></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9pt">&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">return</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000"> left.Where(l =&gt; !right.Where(r =&gt; predicate(l, r)).Any());</font></span></font></p>  <p style="line-height: 13pt; list-style-type: disc; margin: 0in 0in 10pt 0.5in" class="MsoNormal"><span style="line-height: 10pt; font-family: ; color: ; font-size: "><font face="Consolas"><font style="font-size: 9pt" color="#000000">}</font></font></span></p>  <p style="line-height: 13pt; list-style-type: disc; margin: 0in 0in 10pt" class="MsoNormal"><font face="Calibri"><font style="font-size: 11pt" color="#000000">Works fine, but what if I want to issue a database query? In the above example, I’m potentially treating <b style="mso-bidi-font-weight: normal">IQueryable</b> collections as in-memory <b style="mso-bidi-font-weight: normal">IEnumerable </b>collections, which is horribly inefficient. LINQ to Objects will scan the contents of the <b style="mso-bidi-font-weight: normal">left</b> collection and for every element will scan the contents of <b style="mso-bidi-font-weight: normal">right</b> collection until it finds an element matching the <b style="mso-bidi-font-weight: normal">predicate</b>. Let’s change the operator slightly to address this concern:</font></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><font color="#0000ff"><font style="font-size: 9pt">public</font></font></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#000000"> </font></span><span style="font-family: ; color: ; font-size: "><font color="#0000ff">static</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000"> <span style="mso-highlight: yellow"><font style="background-color: #ffff00">IQueryable&lt;TLeft&gt;</font></span> LeftAntiSemiJoin&lt;TLeft, TRight&gt;(</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9pt">&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">this</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000"> <span style="mso-highlight: yellow"><font style="background-color: #ffff00">IQueryable&lt;TLeft&gt;</font></span> left, </font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: ; color: ; font-size: "><font face="Consolas"><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160; </font></span><font style="font-size: 9pt"><span style="mso-highlight: yellow"><font style="background-color: #ffff00">IQueryable&lt;TRight&gt;</font></span> right, </font></font></font></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160; </font></span><font style="font-size: 9pt">Func&lt;TLeft, TRight, </font></font></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">bool</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000">&gt; predicate)</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: ; color: ; font-size: "><font face="Consolas"><font style="font-size: 9pt" color="#000000">{</font></font></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9pt">&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">return</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000"> left.Where(l =&gt; !right.Where(r =&gt; predicate(l, r)).Any());</font></span></font></p>  <p style="line-height: 13pt; list-style-type: disc; margin: 0in 0in 10pt 0.5in" class="MsoNormal"><span style="line-height: 10pt; font-family: ; color: ; font-size: "><font face="Consolas"><font style="font-size: 9pt" color="#000000">}</font></font></span></p>  <p style="line-height: 13pt; list-style-type: disc; margin: 0in 0in 10pt" class="MsoNormal"><font face="Calibri"><font style="font-size: 11pt" color="#000000">Unfortunately, I now get a <b style="mso-bidi-font-weight: normal">NotSupportedException</b> at runtime from LINQ to SQL: “<i style="mso-bidi-font-style: normal">Method 'System.Object DynamicInvoke(System.Object[])' has no supported translation to SQL.</i>” The problem is the opaque <b style="mso-bidi-font-weight: normal">predicate</b> argument, which cannot be evaluated remotely on SQL Server. So let’s turn the <b style="mso-bidi-font-weight: normal">predicate</b> into a lambda expression:</font></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><font color="#0000ff"><font style="font-size: 9pt">public</font></font></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#000000"> </font></span><span style="font-family: ; color: ; font-size: "><font color="#0000ff">static</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000"> IQueryable&lt;TLeft&gt; LeftAntiSemiJoin&lt;TLeft, TRight&gt;(</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9pt">&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">this</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000"> IQueryable&lt;TLeft&gt; left, </font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: ; color: ; font-size: "><font face="Consolas"><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160; </font></span><font style="font-size: 9pt">IQueryable&lt;TRight&gt; right, </font></font></font></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160; </font></span><font style="font-size: 9pt"><span style="mso-highlight: yellow"><font style="background-color: #ffff00">Expression&lt;Func&lt;TLeft, TRight, </font></span></font></font></span><font style="font-size: 9pt"><font style="background-color: #ffff00"><span style="font-family: ; color: ; font-size: ; mso-highlight: yellow"><font color="#0000ff">bool</font></span><span style="font-family: ; color: ; font-size: ; mso-highlight: yellow"><font color="#000000">&gt;&gt; predicate</font></span></font></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000">)</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: ; color: ; font-size: "><font face="Consolas"><font style="font-size: 9pt" color="#000000">{</font></font></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9pt">&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">return</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000"> left.Where(l =&gt; !right.Where(r =&gt; predicate(l, r)).Any());</font></span></font></p>  <p style="line-height: 13pt; list-style-type: disc; margin: 0in 0in 10pt 0.5in" class="MsoNormal"><span style="line-height: 10pt; font-family: ; color: ; font-size: "><font face="Consolas"><font style="font-size: 9pt" color="#000000">}</font></font></span></p>  <p style="line-height: 13pt; list-style-type: disc; margin: 0in 0in 10pt" class="MsoNormal"><font face="Calibri"><font style="font-size: 11pt" color="#000000">Still no luck: this time the C# compiler complains that “<i style="mso-bidi-font-style: normal">'predicate' is a 'variable' but is used like a 'method'</i>”. We’ll need to manually inline the predicate expression instead:</font></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><font color="#0000ff"><font style="font-size: 9pt">public</font></font></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#000000"> </font></span><span style="font-family: ; color: ; font-size: "><font color="#0000ff">static</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000"> IQueryable&lt;TLeft&gt; LeftAntiSemiJoin&lt;TLeft, TRight&gt;(</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: ; color: ; font-size: "><font face="Consolas"><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160; </font></span><font style="font-size: 9pt">IQueryable&lt;TLeft&gt; left, </font></font></font></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: ; color: ; font-size: "><font face="Consolas"><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160; </font></span><font style="font-size: 9pt">IQueryable&lt;TRight&gt; right, </font></font></font></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160; </font></span><font style="font-size: 9pt">Expression&lt;Func&lt;TLeft, TRight, </font></font></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">bool</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000">&gt;&gt; predicate)</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: ; color: ; font-size: "><font face="Consolas"><font style="font-size: 9pt" color="#000000">{</font></font></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9pt">&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">var</font></span><span style="font-family: ; color: ; font-size: "><font color="#000000"> leftPrm = predicate.Parameters[</font></span><span style="font-family: ; color: ; font-size: "><font color="#c81efa">0</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000">];</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9pt">&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">var</font></span><span style="font-family: ; color: ; font-size: "><font color="#000000"> rightPrm = predicate.Parameters[</font></span><span style="font-family: ; color: ; font-size: "><font color="#c81efa">1</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000">];</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font face="Consolas"><font style="font-size: 9pt" color="#000000">&#160;&#160;&#160; </font></font></span></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9pt">&#160;&#160;&#160; </font></font></span></span><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#008000">// retrieve methods</font></span></font><span style="font-family: ; color: ; font-size: "></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160; </font></span><font style="font-size: 9pt">Func&lt;IQueryable&lt;TRight&gt;, </font></font></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">bool</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000">&gt; anyDelegate = Queryable.Any;</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9pt">&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">var </font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000">anyMethod = anyDelegate.Method;</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160; </font></span><font style="font-size: 9pt">Func&lt;IQueryable&lt;TRight&gt;, Expression&lt;Func&lt;TRight, </font></font></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">bool</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000">&gt;&gt;, IQueryable&lt;TRight&gt;&gt; whereDelegate = Queryable.Where;</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9pt">&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">var</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000"> whereMethod = whereDelegate.Method;</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font face="Consolas"><font style="font-size: 9pt" color="#000000">&#160;&#160;&#160; </font></font></span></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9pt">&#160;&#160;&#160; </font></font></span></span><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#008000">// l =&gt; !right.Where(r =&gt; predicate(l, r)).Any()</font></span></font><span style="font-family: ; color: ; font-size: "></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9pt">&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">var</font></span><span style="font-family: ; color: ; font-size: "><font color="#000000"> leftPredicate = Expression.Lambda&lt;Func&lt;TLeft, </font></span><span style="font-family: ; color: ; font-size: "><font color="#0000ff">bool</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000">&gt;&gt;(</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: ; color: ; font-size: "><font face="Consolas"><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt">Expression.Not(</font></font></font></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: ; color: ; font-size: "><font face="Consolas"><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt">Expression.Call(anyMethod,</font></font></font></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: ; color: ; font-size: "><font face="Consolas"><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt">Expression.Call(whereMethod,</font></font></font></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: ; color: ; font-size: "><font face="Consolas"><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt">Expression.Constant(right),</font></font></font></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt">Expression.Lambda&lt;Func&lt;TRight, </font></font></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">bool</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000">&gt;&gt;(predicate.Body, rightPrm)))),</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: ; color: ; font-size: "><font face="Consolas"><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt">leftPrm);</font></font></font></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font face="Consolas"><font style="font-size: 9pt" color="#000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9pt">&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">return</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000"> left.Where(leftPredicate);</font></span></font></p>  <p style="line-height: 13pt; list-style-type: disc; margin: 0in 0in 10pt 0.5in" class="MsoNormal"><span style="line-height: 10pt; font-family: ; color: ; font-size: "><font face="Consolas"><font style="font-size: 9pt" color="#000000">}</font></font></span></p>  <p style="line-height: 13pt; list-style-type: disc; margin: 0in 0in 10pt" class="MsoNormal"><font face="Calibri"><font style="font-size: 11pt" color="#000000">A working solution! A couple of observations:</font></font></p>  <p style="line-height: 13pt; list-style-type: disc; text-indent: -0.25in; margin: 0in 0in 0pt 0.5in; mso-list: l0 level1 lfo1" class="MsoListParagraphCxSpFirst"><font color="#000000"><span style="font-family: ; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol"><span style="mso-list: ignore"><font face="Symbol"><font style="font-size: 11pt">·</font></font><span><font face="Times New Roman"><font style="font-size: 7pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span></span></span><font face="Calibri"><font style="font-size: 11pt">Notice that this approach does not rely on <b style="mso-bidi-font-weight: normal">Expression.Invoke</b>. Some LINQ providers (LINQ to Entities and LINQ to StreamInsight among them) do not support invocation expressions. By reusing the existing parameter expressions (<b style="mso-bidi-font-weight: normal">leftPrm </b>and <b style="mso-bidi-font-weight: normal">rightPrm</b>) from the <b style="mso-bidi-font-weight: normal">predicate</b> argument within the constructed expression, I avoid the need to “rebind” any parameters. See my </font></font></font><font style="font-size: 11pt"><a style="cursor: auto" href="http://blogs.msdn.com/b/meek/archive/2008/12/07/invocationexpression-and-linq-to-entities.aspx"><font color="#0000ff" face="Calibri"><u>earlier post</u></font></a></font><font face="Calibri"><font style="font-size: 11pt" color="#000000"> for a more general workaround to the invocation expression limitation.</font></font></p>  <p style="line-height: 13pt; list-style-type: disc; text-indent: -0.25in; margin: 0in 0in 10pt 0.5in; mso-list: l0 level1 lfo1" class="MsoListParagraphCxSpLast"><font color="#000000"><span style="font-family: ; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol"><span style="mso-list: ignore"><font face="Symbol"><font style="font-size: 11pt">·</font></font><span><font face="Times New Roman"><font style="font-size: 7pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span></span></span><font face="Calibri"><font style="font-size: 11pt">One of my favorite tricks: rather than relying on <b style="mso-bidi-font-weight: normal">Type.GetMethod </b>and <b style="mso-bidi-font-weight: normal">MethodInfo.MakeGenericMethod</b>, I’m retrieving <b style="mso-bidi-font-weight: normal">MethodInfo</b>s from typed delegates. More robust than the more conventional solution because the C# compiler statically binds to the appropriate method signature.</font></font></font></p>  <p style="line-height: 13pt; list-style-type: disc; margin: 0in 0in 10pt" class="MsoNormal"><font face="Calibri"><font style="font-size: 11pt" color="#000000">A gotcha: if you attempt to use this (or other) operator macros within a lambda body, some LINQ providers will balk at the unrecognized method. For instance, LINQ to SQL is fine with the following usage of the macro because it doesn’t see the <b style="mso-bidi-font-weight: normal">LeftAntiSemiJoin</b> method call in the resulting expression tree (the macro is expanded before reaching LINQ to SQL):</font></font></p>  <p style="line-height: 13pt; list-style-type: disc; margin: 0in 0in 10pt 0.5in" class="MsoNormal"><font face="Consolas"><span style="line-height: 10pt; font-family: ; color: ; font-size: "><font color="#0000ff"><font style="font-size: 9pt">var</font></font></span><span style="line-height: 10pt; font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000"> query1 = LeftAntiSemiJoin(Categories, Products, (c, p) =&gt; c.Cid == p.Cid);</font></span></font></p>  <p style="line-height: 13pt; list-style-type: disc; margin: 0in 0in 10pt" class="MsoNormal"><font face="Calibri"><font style="font-size: 11pt" color="#000000">but complains about an unsupported method when the same macro appears within an expression:</font></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="mso-spacerun: yes"><font face="Calibri"><font style="font-size: 11pt" color="#000000">&#160;</font></font></span><font face="Consolas"><span style="font-family: ; color: ; font-size: "><font color="#0000ff"><font style="font-size: 9pt">var</font></font></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#000000"> query2 = </font></span><span style="font-family: ; color: ; font-size: "><font color="#0000ff">from</font></span><span style="font-family: ; color: ; font-size: "><font color="#000000"> c1 </font></span><span style="font-family: ; color: ; font-size: "><font color="#0000ff">in</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000"> Categories</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">from</font></span><span style="font-family: ; color: ; font-size: "><font color="#000000"> c2 </font></span><span style="font-family: ; color: ; font-size: "><font color="#0000ff">in</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000"> LeftAntiSemiJoin(Categories, Products, (c, p) =&gt; c.Cid == p.Cid)</font></span></font></p>  <p style="line-height: 13pt; list-style-type: disc; margin: 0in 0in 10pt 0.5in" class="MsoNormal"><font face="Consolas"><span style="line-height: 10pt; font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt"><span style="line-height: 10pt; font-family: ; color: ; font-size: "><font color="#0000ff">select</font></span></font><span style="line-height: 10pt; font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000"> c1;</font></span></font></p>  <p style="line-height: 13pt; list-style-type: disc; margin: 0in 0in 10pt" class="MsoNormal"><font face="Calibri"><font style="font-size: 11pt" color="#000000">You can work around this (intentional) limitation by assigning the expanded query to a local variable (we’ve already assigned the expanded version to <b style="mso-bidi-font-weight: normal">query1</b> so we can reuse it here):</font></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><font color="#0000ff"><font style="font-size: 9pt">var</font></font></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#000000"> query3 = </font></span><span style="font-family: ; color: ; font-size: "><font color="#0000ff">from</font></span><span style="font-family: ; color: ; font-size: "><font color="#000000"> c1 </font></span><span style="font-family: ; color: ; font-size: "><font color="#0000ff">in</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000"> Categories</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt"><span style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">from</font></span><span style="font-family: ; color: ; font-size: "><font color="#000000"> c2 </font></span><span style="font-family: ; color: ; font-size: "><font color="#0000ff">in</font></span></font><span style="font-family: ; color: ; font-size: "><font color="#000000"><font style="font-size: 9pt"> </font><span style="mso-highlight: yellow"><font style="background-color: #ffff00; font-size: 9pt">query1</font></span></font></span></font></p>  <p style="line-height: 13pt; list-style-type: disc; margin: 0in 0in 10pt 0.5in" class="MsoNormal"><font face="Consolas"><span style="line-height: 10pt; font-family: ; color: ; font-size: "><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt"><span style="mso-spacerun: yes">&#160;</span><span style="mso-spacerun: yes">&#160;&#160; </span></font></font></span><font style="font-size: 9pt"><span style="line-height: 10pt; font-family: ; color: ; font-size: "><font color="#0000ff">select</font></span></font><span style="line-height: 10pt; font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000"> c1;</font></span></font></p>  <p style="line-height: 13pt; list-style-type: disc; margin: 0in 0in 10pt" class="MsoNormal"><font face="Calibri"><font style="font-size: 11pt" color="#000000">StreamInsight implements a temporal version of LASJ as well. It’s similar to the familiar SQL Server operator but returns left-hand events <i style="mso-bidi-font-style: normal">for time intervals during which</i> no corresponding right-hand event exists. The corresponding macro operator is shown below. The only significant difference from the <b style="mso-bidi-font-weight: normal">IQueryable</b> version is that the <b style="mso-bidi-font-weight: normal">IsEmpty</b> stream operator is used in place of the <b style="mso-bidi-font-weight: normal">Any</b> sequence operator:</font></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><font color="#0000ff"><font style="font-size: 9pt">public</font></font></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#000000"> </font></span><span style="font-family: ; color: ; font-size: "><font color="#0000ff">static</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000"> CepStream&lt;TLeft&gt; LeftAntiSemiJoin&lt;TLeft, TRight&gt;(</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160; </font></span><font style="font-size: 9pt"><span style="mso-spacerun: yes">&#160; </span>CepStream&lt;TLeft&gt; left, CepStream&lt;TRight&gt; right, Expression&lt;Func&lt;TLeft, TRight, </font></font></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">bool</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000">&gt;&gt; predicate)</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: ; color: ; font-size: "><font face="Consolas"><font style="font-size: 9pt" color="#000000">{</font></font></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9pt">&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">var</font></span><span style="font-family: ; color: ; font-size: "><font color="#000000"> leftPrm = predicate.Parameters[</font></span><span style="font-family: ; color: ; font-size: "><font color="#c81efa">0</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000">];</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9pt">&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">var</font></span><span style="font-family: ; color: ; font-size: "><font color="#000000"> rightPrm = predicate.Parameters[</font></span><span style="font-family: ; color: ; font-size: "><font color="#c81efa">1</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000">];</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font face="Consolas"><font style="font-size: 9pt" color="#000000">&#160;&#160;&#160; </font></font></span></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9pt">&#160;&#160;&#160; </font></font></span></span><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#008000">// retrieve methods</font></span></font><span style="font-family: ; color: ; font-size: "></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160; </font></span><font style="font-size: 9pt">Func&lt;CepStream&lt;TRight&gt;, </font></font></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">bool</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000">&gt; isEmptyDelegate = CepStream.IsEmpty;</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9pt">&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">var</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000"> isEmptyMethod = isEmptyDelegate.Method;</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160; </font></span><font style="font-size: 9pt">Func&lt;CepStream&lt;TRight&gt;, Expression&lt;Func&lt;TRight, </font></font></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">bool</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000">&gt;&gt;, CepStream&lt;TRight&gt;&gt; whereDelegate = CepStream.Where;</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9pt">&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">var</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000"> whereMethod = whereDelegate.Method;</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font face="Consolas"><font style="font-size: 9pt" color="#000000">&#160;&#160;&#160; </font></font></span></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9pt">&#160;&#160;&#160; </font></font></span></span><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#008000">// l =&gt; right.Where(r =&gt; predicate(l, r)).IsEmpty()</font></span></font><span style="font-family: ; color: ; font-size: "></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9pt">&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">var</font></span><span style="font-family: ; color: ; font-size: "><font color="#000000"> leftPredicate = Expression.Lambda&lt;Func&lt;TLeft, </font></span><span style="font-family: ; color: ; font-size: "><font color="#0000ff">bool</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000">&gt;&gt;(</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: ; color: ; font-size: "><font face="Consolas"><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt">Expression.Call(isEmptyMethod,</font></font></font></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: ; color: ; font-size: "><font face="Consolas"><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt">Expression.Call(whereMethod,</font></font></font></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: ; color: ; font-size: "><font face="Consolas"><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt">Expression.Constant(right),</font></font></font></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt">Expression.Lambda&lt;Func&lt;TRight, </font></font></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">bool</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000">&gt;&gt;(predicate.Body, rightPrm))),</font></span></font></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: ; color: ; font-size: "><font face="Consolas"><font color="#000000"><span style="mso-spacerun: yes"><font style="font-size: 9pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt">leftPrm);</font></font></font></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font face="Consolas"><font style="font-size: 9pt" color="#000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span></span></p>  <p style="line-height: normal; list-style-type: disc; margin: 0in 0in 0pt 0.5in; text-autospace: ; mso-layout-grid-align: none" class="MsoNormal"><font face="Consolas"><span style="font-family: ; color: ; font-size: "><span style="mso-spacerun: yes"><font color="#000000"><font style="font-size: 9pt">&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt"><span style="font-family: ; color: ; font-size: "><font color="#0000ff">return</font></span></font><span style="font-family: ; color: ; font-size: "><font style="font-size: 9pt" color="#000000"> left.Where(leftPredicate);</font></span></font></p>  <p style="line-height: 13pt; list-style-type: disc; margin: 0in 0in 10pt 0.5in" class="MsoNormal"><span style="line-height: 10pt; font-family: ; color: ; font-size: "><font face="Consolas"><font style="font-size: 9pt" color="#000000">}</font></font></span></p>  <p style="line-height: 13pt; list-style-type: disc; margin: 0in 0in 10pt" class="MsoNormal"><font face="Calibri"><font style="font-size: 11pt" color="#000000">&#160;</font></font></p>]]></content:encoded>
    </item>
    <item>
      <title>StreamInsight Sequence Integration: Five Easy Pieces</title>
      <link>https://docs.microsoft.com/archive/blogs/meek/streaminsight-sequence-integration-five-easy-pieces</link>
      <pubDate>Wed, 27 Oct 2010 15:44:29 GMT</pubDate>
      <dc:creator><![CDATA[Colin Meek]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/meek/2010/10/27/streaminsight-sequence-integration-five-easy-pieces/</guid>
      <description><![CDATA[Over the past few weeks, I’ve spent time building a handful of applications using the new sequence...]]></description>
      <content:encoded><![CDATA[<p>Over the past few weeks, I’ve spent time building a handful of applications using the new sequence integration APIs in StreamInsight 1.1. I think StreamInsight veterans will be pleasantly surprised at the seamlessness of the experience! If you’re new to StreamInsight, now’s your chance to quickly build a temporally aware application. In this post, I’ll walk through five components of a typical end-to-end StreamInsight query, from event source to event sink.</p>  <p>First, take some time to download the latest version and kick the tires:</p>  <ul>   <li><a href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=9cfb2d51-5ff4-4491-b0e5-b386f32c0992&amp;displaylang=en">.NET 4.0 Download</a> (.NET 3.5 SP1 is sufficient if you are not using <code>IObservable&lt;&gt;</code> event sources or event sinks). </li>    <li><a title="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=a2d0e7aa-2a42-4b02-855b-78804110026a" href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=a2d0e7aa-2a42-4b02-855b-78804110026a">StreamInsight 1.1 Download</a> </li>    <li><a title="http://streaminsight.codeplex.com/" href="http://streaminsight.codeplex.com/releases/view/46435">StreamInsight Samples</a> </li> </ul>  <p>Now you're ready to build a StreamInsight application. A few considerations:</p>  <ul>   <li>If you are creating a .NET 4.0 application, make sure it is targeting the full .NET 4.0 profile, not the client profile. Configure the target framework through project properties in Visual Studio 2010. </li>    <li>Add a reference to the <code>Microsoft.ComplexEventProcessing</code> assembly. If you plan on using an <code>IObservable&lt;&gt;</code> event source or event sink, add a reference to the <code>Microsoft.ComplexEventProcessing.Observable</code> assembly as well. </li>    <li>Use the <code>Microsoft.ComplexEventProcessing</code> and <code>Microsoft.ComplexEventProcessing.Linq</code> namespaces. The first allows you to embed and manage a StreamInsight server. The second exposes the StreamInsight LINQ dialect. </li> </ul>  <p>Now we'll travel downstream, using the SequenceIntegration\Northwind sample as our guide.</p>  <h1>Event Sources</h1>  <p>Event sources for a query can be based on custom input adapters, other StreamInsight queries, or – new in version 1.1 – .NET <code>IObservable&lt;&gt;</code> and <code>IEnumerable&lt;&gt;</code> sequences. The good news: in the .NET world, <code>IEnumerable&lt;&gt;</code> <em>pull</em>-based sequences are pervasive. SQL, OData, Sharepoint, you name it. Better news from the perspective of a Complex Event Processing system: asynchronous and <em>push</em>-based sequences can be easily exposed via the <code>IObservable&lt;&gt;</code> interface, particularly if you take advantage of the .NET Reactive Framework (<a href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx">Rx</a>).</p>  <p>In this simple example, we use two OData service queries as our event sources:</p> <code>   <pre>NorthwindEntities northwind = new NorthwindEntities(
    new Uri(&quot;http://services.odata.org/Northwind/Northwind.svc&quot;));

// Issue OData queries to determine start and end times for orders.
// So that the sources behave like temporal streams, we order by the
// corresponding dates.
var ordersWithRegions =
    from o in northwind.Orders
    where o.ShipRegion != null
    select o;

var orderStartTimes =
    from o in ordersWithRegions
    where o.OrderDate != null
    orderby o.OrderDate
    select new { StartTime = (DateTime)o.OrderDate, o.OrderID, o.ShipRegion };

var orderEndTimes =
    from o in ordersWithRegions
    where o.ShippedDate != null
    orderby o.ShippedDate
    select new { EndTime = (DateTime)o.ShippedDate, o.OrderID };</pre>
</code>

<p>The <code>orderStartTimes</code> and <code>orderEndTimes</code> queries implement <code>IEnumerable&lt;&gt;</code> which makes them suitable event sources for StreamInsight.</p>

<h1>Temporal streams</h1>

<p>An <code>IObservable&lt;&gt;</code> or <code>IEnumerable&lt;&gt;</code> event source can feed a temporal stream. A temporal stream is a sequence of events annotated with temporal information: timestamps for events and punctuation indicating when a particular point in time has been committed. In the above example, we have two sources <code>orderStartTimes</code> and <code>orderEndTimes</code> each including timestamp fields – <code>StartTime</code> and <code>EndTime</code> respectively – as well as a commitment based on <code>orderby</code> clauses that events timestamps are monotonic. We describe these temporal characteristics to StreamInsight using the <code>ToPointStream</code> method:</p>
<code>
  <pre>// Map OData queries to StreamInsight inputs
var startStream = orderStartTimes.ToPointStream(orderApp, s =&gt;
    PointEvent.CreateInsert(s.StartTime, s), 
    AdvanceTimeSettings.IncreasingStartTime);

var endStream = orderEndTimes.ToPointStream(orderApp, e =&gt;
    PointEvent.CreateInsert(e.EndTime, e), 
    AdvanceTimeSettings.IncreasingStartTime);</pre>
</code>

<p>The arguments to the <code>ToPointStream</code> extension method are described below:</p>

<ul>
  <li><em>IEnumerable&lt;T&gt; source</em>: the event source. In this case, the source is an OData query. </li>

  <li><em>Application application</em>: the StreamInsight application that will host the temporal query. </li>

  <li><em>Func&lt;TInput, PointEvent&lt;TPayload&gt; selector</em>: takes an element of the source and turns it into a temporal event. The first selector loosely reads: “<em>the event </em><code>s</code><em> happened at the point in time </em><code>s.StartTime</code>”. The first argument to <code>PointEvent.CreateInsert</code> indicates the timestamp for the event, and the second argument describe the payload of the event – in this case the entire row. 

    <br />

    <br /><em>Aside: StreamInsight has restrictions on payload types. Basically, the payload must consist of a type or struct with only “primitive” (string, number, etc.) fields and properties. See the </em>Payload Field Requirements<em> section @ <a title="http://msdn.microsoft.com/en-us/library/ee378905.aspx" href="http://msdn.microsoft.com/en-us/library/ee378905.aspx">http://msdn.microsoft.com/en-us/library/ee378905.aspx</a> for details. The event selector can be used to reshape your inputs to a supported payload type. </em></li>

  <li><em>AdvanceTimeSetting advanceTimeSettings</em>: an optional parameter that describes a policy for automatically generating punctuation. In the above example, we indicate that timestamps are increasing in the input. StreamInsight can then automatically generate Current Time Increment (CTI) punctuation for each event: “<em>we commit to everything before the event’s timestamp</em>”. </li>

  <li><em>string streamName</em>: an optional parameter allowing you to assign a name to the input stream. I have not specified a stream name in the above example. This feature is particularly useful if you’re importing punctuations from one stream to another (see the SequenceIntegration\PerformanceCounters sample for instance). </li>
</ul>

<p>Note that there are several variations on the <code>To*Stream</code> method supporting <code>IObservable&lt;&gt;</code> or <code>IEnumerable&lt;&gt;</code> event sources and the shaping of point, interval or edge data.</p>

<p>Take a look at Cip’s <a href="http://blogs.msdn.com/b/streaminsight/archive/2010/07/23/time-in-streaminsight-i.aspx">Time in StreamInsight</a> series for more information. </p>

<h1></h1>

<h1>Temporal query</h1>

<p>Now that we have described the temporal characteristics of our event sources, we can compose a StreamInsight query. I’ll simply copy the code here without too much explanation since I’m focused on data ingress and egress in this post:</p>
<code>
  <pre>// Use clip to synthesize events lasting from the start of each order to the end
// of each order.
var clippedStream = startStream
    .AlterEventDuration(e =&gt; TimeSpan.MaxValue)
    .ClipEventDuration(endStream, (s, e) =&gt; s.OrderID == e.OrderID);

// Count the number of coincident orders per region
var counts = from o in clippedStream
                group o by o.ShipRegion into g
                from win in g.SnapshotWindow(SnapshotWindowOutputPolicy.Clip)
                select new { ShipRegion = g.Key, Count = win.Count() };

// Display output whenever there are more than 2 active orders in a region.
const int threshold = 2;
var query = from c in counts
            where c.Count &gt; threshold
            select c;</pre>
</code>

<h1>Event sink</h1>

<p>Creating an event sink is straightforward. Several extension methods support the transformation of a temporal query (<code>CepStream&lt;&gt;</code>) to an event sink, with support for permutations of <code>IObservable</code> or <code>IEnumerable</code> sequences and <code>TPayload</code>, <code>PointEvent&lt;TPayload&gt;</code>, <code>IntervalEvent&lt;TPayload&gt;</code> or <code>EdgeEvent&lt;TPayload&gt;</code> elements. In the following example, we translate the query to a sequence of interval events using <code>ToIntervalEnumerable</code>. We then filter out insert events – skipping CTI punctuation – and project out relevant temporal and payload fields:</p>
<code>
  <pre>// Map the query to an IEnumerable sink
var sink = from i in query.ToIntervalEnumerable()
           where i.EventKind == EventKind.Insert
           select new { i.StartTime, i.EndTime, i.Payload.Count, i.Payload.ShipRegion };</pre>
</code>

<h1>Consuming results</h1>

<p>Now that we’re back in the world of .NET sequences, there are many possibilities for consuming the results. For now, we’ll just write the event sink contents to the console:</p>
<code>
  <pre>foreach (var r in sink)
{
    Console.WriteLine(r);
}</pre>
</code>

<p>Interestingly, calling <code>GetEnumerator</code> on <code>sink</code> triggers a sequence of actions:</p>

<ol>
  <li>A stream query is deployed to the embedded server and started, which implicitly spins up the query inputs, triggering… </li>

  <li>calls to <code>GetEnumerator</code> on the event sources we defined earlier, which… </li>

  <li>causes a query against the OData service to be executed. </li>
</ol>

<p>StreamInsight queries now composes seamlessly with other LINQ providers! In fact, if you review the code above you can see that we’ve leveraged LINQ to OData, LINQ to Objects, and LINQ to StreamInsight. The SequenceIntegration samples linked above illustrate some other integration possibilities as well:</p>

<ul>
  <li>A WPF control that observers an <code>IObservable</code> event sink. </li>

  <li>An <code>IObservable </code>event source that polls performance counters. </li>

  <li>An <code>IEnumerable </code>event source that reads the contents of a file.</li>
</ul>

<p>There – you have the five easy pieces! Comes with a side-order of toast.</p>]]></content:encoded>
    </item>
    <item>
      <title>Using and Discovering StreamInsight Instances</title>
      <link>https://docs.microsoft.com/archive/blogs/meek/using-and-discovering-streaminsight-instances</link>
      <pubDate>Tue, 26 Oct 2010 21:11:00 GMT</pubDate>
      <dc:creator><![CDATA[Colin Meek]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/meek/2010/10/26/using-and-discovering-streaminsight-instances/</guid>
      <description><![CDATA[Embedding a StreamInsight server instance in your application is easy:
 
using (var server =...]]></description>
      <content:encoded><![CDATA[<p>Embedding a StreamInsight server instance in your application is easy:</p>
<p> <code>
<pre>using (var server = Server.Create("Default"))
{
   &hellip;
}</pre>
</code></p>
<p>But there&rsquo;s a hitch. What does&ldquo;Default&rdquo; mean? If you&rsquo;ve spent time with the StreamInsight samples, you might imagine the &ldquo;Default&rdquo; incantation has a special meaning. The verification tests we run on the StreamInsight team require that you install a server instance named &ldquo;Default&rdquo;, and &ndash; not coincidentally &ndash; this tends to be the choice of instance name in StreamInsight samples as well. Let&rsquo;s look at how to work with, discover, and understand server instances. First, some general notes:</p>
<ul>
<li>A SQL Server 2008 R2 license is required to run StreamInsight, either as a dedicated service or an embedded server. The instance name passed to the <a href="http://technet.microsoft.com/en-us/library/ff518567.aspx">Server.Create</a> method associates the embedded server with a particular license. </li>
<li>A free evaluation edition is available as well. See the <a href="http://blogs.msdn.com/b/streaminsight/">StreamInsight team blog</a> for details. </li>
<li>A SQL Server 2008 R2 installation is <span style="text-decoration: underline;">not</span> required. StreamInsight uses standalone installers. </li>
<li>There are two installers. One includes only the client libraries that can be used to connect to an existing StreamInsight service. The other installs the same libraries but additionally registers a server instance and (optionally) a service. The stand-alone service is optional because some users may want to just embed StreamInsight. </li>
<li>A StreamInsight server instance carefully controls resource allocation &ndash; memory, cores &ndash; for the stream queries it hosts. As a result, it is best to embed a single server instance in your application. Otherwise, the instances will compete with each other for resources, and no one wins. </li>
</ul>
<p>If you write an application that embeds StreamInsight, you need to know the name of the corresponding instance. But what if your application is running on a strange machine? The usual official advice applies: use app/web config files to store the instance name for <a href="http://technet.microsoft.com/en-us/library/ff518567.aspx">Server.Create</a>. You can configure the management service URI used by <a href="http://technet.microsoft.com/en-us/library/ee362557.aspx">Server.Connect</a> in the same way.</p>
<p>Now the unofficial advice.</p>
<p>Server instance information is stored in the registry. Launch regedit and take a look under <i>HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft StreamInsight</i> to see examples. Looking up values in the registry can be hazardous, particularly if you want to be robust to permutations of 32/64-bit operating system, StreamInsight install, and .NET application. Shahar Prish&rsquo;s <a href="http://blogs.msdn.com/b/cumgranosalis/archive/2005/12/09/win64registrypart1.aspx">advice</a> on the topic gives an idea of the challenges, but fortunately there is an easier alternative in .NET 4.0. The new <a href="http://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey.openbasekey.aspx">OpenBaseKey</a> method allows you to explicitly select a 64-bit registry view (I was pretty excited to find this &ndash; as far as I can tell, there were no announcements of this extension to the .NET registry key APIs). Here&rsquo;s a utility you can use to reliably retrieve StreamInsight instance data:</p>
<p>
<code>
<pre>public struct InstanceInfo
{
    const string InstanceKeyPrefix = "MSSI.";

    public readonly string Name;
    public readonly Version Version;
    public readonly string Edition;

    InstanceInfo(string name, RegistryKey instanceKey)
    {
        Name = name;
        System.Version.TryParse((string)instanceKey.GetValue("Version", string.Empty), out Version);
        Edition = (string)instanceKey.GetValue("Edition");
    }

    public override string ToString()
    {
        return new { Version, Name, Edition }.ToString();
    }

    public static IEnumerable<instanceinfo> GetInstalledInstances()
    {
        // On a 64-bit machine, StreamInsight always registers instances in the 64-bit registry hive
        // even for 32-bit installations. In addition, we need to ensure that we access the correct
        // hive even if we're running a 32-bit .NET application on a 64-bit machine.
        using (var baseKey =
            RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
            ?? RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32))
        {
            using (var siKey = baseKey.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft StreamInsight"))
            {
                if (null == siKey) yield break;

                foreach (var subKeyName in siKey.GetSubKeyNames())
                {
                    if (subKeyName.StartsWith(InstanceKeyPrefix, StringComparison.Ordinal))
                    {
                        using (var instanceKey = siKey.OpenSubKey(subKeyName))
                        {
                            yield return new InstanceInfo(subKeyName.Substring(InstanceKeyPrefix.Length), instanceKey);
                        }
                    }
                }
            }
        }
    }
}</instanceinfo></pre>
</code></p>
<p>I&rsquo;ll call out one trick in the implementation: the ToString() override uses an anonymous type to format output. The compiler auto-generates a ToString() implementation for anonymous types, so we can write:</p>
<p>
<code>
<pre>new { Version, Name, Edition }.ToString()</pre>
</code></p>
<p>instead of the more familiar but error-prone:</p>
<p>
<code>
<pre>string.Format(System.Globalization.CultureInfo.InvariantCulture,
    "Name = {0}, Version = {1}, Edition = {2}",
    Name, Version, Edition)</pre>
</code></p>
<p>Let me know if you have any questions on StreamInsight instances! If I can&rsquo;t answer them I can hopefully find someone who can.</p>]]></content:encoded>
    </item>
    <item>
      <title>Scripting StreamInsight queries</title>
      <link>https://docs.microsoft.com/archive/blogs/meek/scripting-streaminsight-queries</link>
      <pubDate>Tue, 31 Aug 2010 13:05:00 GMT</pubDate>
      <dc:creator><![CDATA[Colin Meek]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/meek/2010/08/31/scripting-streaminsight-queries/</guid>
      <description><![CDATA[Over the past couple of weeks, a handful of people have asked for help dynamically creating...]]></description>
      <content:encoded><![CDATA[<p>Over the past couple of weeks, a handful of people have asked for help dynamically creating StreamInsight queries. I usually scrawl some boxes and arrows on the whiteboard and say &ldquo;you could try something like this&hellip;&rdquo; My hand-waving hasn&rsquo;t been very helpful. I&rsquo;ll write some code instead&hellip;</p>
<p>A StreamInsight query includes a &ldquo;template&rdquo; definition that essentially describes the operator tree. Normally, a developer describes a template using a LINQ query (StreamInsight understands a LINQ dialect for streaming/temporal queries). The LINQ query is then translated into an XML document (schema details are available <a target="_blank" href="http://schemas.microsoft.com/ComplexEventProcessing/2010/01/Metadata/">here</a>) which is compiled and executed by the StreamInsight engine. In one sense, you can see that StreamInsight already supports dynamic queries since an application can construct a query operator tree using the XML specification language directly. A visual design surface or DSL could potentially be used to generate the XML. My preferred general-purpose stream query language is LINQ however, so I&rsquo;ll instead consider what it takes to dynamically generate a LINQ query using StreamInsight.</p>
<p>Anders Hejlsberg&rsquo;s talk on <a target="_blank" href="https://channel9.msdn.com/pdc2008/TL16/">The Future of C#</a> gives a possible answer. Using the proposed C# eval facility, I could write something like:</p>
<div style="padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt; mso-element: para-border-div; mso-border-alt: solid windowtext .5pt; border: windowtext 1pt solid;"><span style="font-family: 'Courier New'; color: green; font-size: 10pt; mso-no-proof: yes;">// initialize an evaluator<o:p></o:p></span>
<p class="MsoNormal"><span style="font-family: 'Courier New'; color: blue; font-size: 10pt; mso-no-proof: yes;">var</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> ev = <span style="color: blue;">new</span> <span style="color: #2b91af;">CSharpEvaluator<o:p></o:p></span></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">{<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>References = { <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">CepStream</span>).Assembly, <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">Expression</span>).Assembly, <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">EventType1</span>).Assembly, },<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>Usings = { <span style="color: #a31515;">"MyNamespace"</span>, <span style="color: #a31515;">"Microsoft.ComplexEventProcessing.Linq"</span>, },<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">};<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; color: green; font-size: 10pt; mso-no-proof: yes;">// add input streams<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">ev.Evaluate(<span style="color: #a31515;">@"var input1 = CepStream&lt;EventType1&gt;.Create(""input1"")"</span>);<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; color: green; font-size: 10pt; mso-no-proof: yes;">// script query<o:p></o:p></span></p>
<p class="MsoNormal"><span style="line-height: 115%; font-family: 'Courier New'; color: blue; font-size: 10pt; mso-no-proof: yes;">var</span><span style="line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> queryDefinition = (<span style="color: #2b91af;">CepStream</span>&lt;<span style="color: #2b91af;">EventType1</span>&gt;)ev.Evaluate(<span style="color: #a31515;">"from i in input1 where i.X &gt; 10 select i"</span>);</span></p>
</div>
<p>&nbsp;</p>
<p>Since I don&rsquo;t want to wait for C# 5.0, I&rsquo;ll use a more specialized &ldquo;scripter&rdquo; tool designed specifically for StreamInsight query templates:</p>
<div style="padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt; mso-element: para-border-div; mso-border-alt: solid windowtext .5pt; border: windowtext 1pt solid;">
<p class="MsoNormal"><span style="font-family: 'Courier New'; color: green; font-size: 10pt; mso-no-proof: yes;">// initialize a scripter<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; color: blue; font-size: 10pt; mso-no-proof: yes;">var</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> scripter = <span style="color: blue;">new</span> <span style="color: #2b91af;">QueryTemplateScripter</span>();<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; color: green; font-size: 10pt; mso-no-proof: yes;">// add input streams<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">scripter.AddStream(<span style="color: #a31515;">"input1"</span>, <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">EventType1</span>));<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; color: green; font-size: 10pt; mso-no-proof: yes;">// script query<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; color: blue; font-size: 10pt; mso-no-proof: yes;">var</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> template = scripter.CreateQueryTemplate(app, <span style="color: #a31515;">"MyTemplate"</span>, <span style="color: blue;">null</span>,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span><span style="color: #a31515;">"from i in input1 where i.X &gt; 10 select i"</span>);</span><o:p></o:p></p>
</div>
<p>&nbsp;</p>
<p>The scripter allows you to:</p>
<ul>
<li>reference assemblies (the required StreamInsight assemblies are referenced by default); </li>
<li>register input streams for your query using the AddStream method; </li>
<li>add &ldquo;using&rdquo; namespaces (the expected namespaces are included by default), and finally; </li>
<li>create a query template based on a LINQ query specification. </li>
</ul>
<p>The scripter essentially inlines the LINQ query into a program containing any event definitions (in the above example, we are merely referencing an existing type so no definition is required) and a query &ldquo;context&rdquo; that passes the LINQ query definition to an existing <a target="_blank" href="http://msdn.microsoft.com/en-us/library/ff518568.aspx">Application.CreateQueryTemplate</a> method. An example of a generated program is shown below:</p>
<div style="padding-bottom: 1pt; padding-left: 4pt; padding-right: 4pt; padding-top: 1pt; mso-element: para-border-div; mso-border-alt: solid windowtext .5pt; border: windowtext 1pt solid;">
<p class="MsoNormal"><span style="font-family: 'Courier New'; color: green; font-size: 10pt; mso-no-proof: yes;">//------------------------------------------------------------------------------<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; color: green; font-size: 10pt; mso-no-proof: yes;">// &lt;auto-generated&gt;<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; color: green; font-size: 10pt; mso-no-proof: yes;">//<span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp; </span>This code was generated by a tool.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; color: green; font-size: 10pt; mso-no-proof: yes;">//<span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp; </span>Runtime Version:2.0.50727.4952<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; color: green; font-size: 10pt; mso-no-proof: yes;">//<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; color: green; font-size: 10pt; mso-no-proof: yes;">//<span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp; </span>Changes to this file may cause incorrect behavior and will be lost if<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; color: green; font-size: 10pt; mso-no-proof: yes;">//<span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp; </span>the code is regenerated.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; color: green; font-size: 10pt; mso-no-proof: yes;">// &lt;/auto-generated&gt;<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; color: green; font-size: 10pt; mso-no-proof: yes;">//------------------------------------------------------------------------------<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; color: green; font-size: 10pt; mso-no-proof: yes;"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; color: blue; font-size: 10pt; mso-no-proof: yes;">namespace</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> @__QueryTemplateScripter<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">{<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">using</span> System;<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">using</span> Microsoft.ComplexEventProcessing;<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">using</span> Microsoft.ComplexEventProcessing.Linq;<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">public</span> <span style="color: blue;">class</span> @__QueryTemplateScripterContext<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">public</span> <span style="color: blue;">static</span> Microsoft.ComplexEventProcessing.<span style="color: #2b91af;">QueryTemplate</span> @__CreateQueryTemplate(Microsoft.ComplexEventProcessing.<span style="color: #2b91af;">Application</span> application, <span style="color: blue;">string</span> name, <span style="color: blue;">string</span> description)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">CepStream</span>&lt;ConsoleApplication1.<span style="color: #2b91af;">EventType1</span>&gt; input1 = <span style="color: #2b91af;">CepStream</span>&lt;ConsoleApplication1.<span style="color: #2b91af;">EventType1</span>&gt;.Create(<span style="color: #a31515;">"input1"</span>);<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">var</span> definition =<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; color: blue; font-size: 10pt; mso-no-proof: yes;">#line</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> 1 "__LinqQueryDefinition"<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;">&nbsp;</span><span style="color: blue;">from</span> i <span style="color: blue;">in</span> input1 <span style="color: blue;">where</span> i.X &gt; 10 <span style="color: blue;">select</span> i<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; color: blue; font-size: 10pt; mso-no-proof: yes;">#line</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> default<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; color: blue; font-size: 10pt; mso-no-proof: yes;">#line</span><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"> hidden<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">;<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">return</span> application.CreateQueryTemplate(name, description, definition);<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p class="MsoNormal"><span style="line-height: 115%; font-family: 'Courier New'; font-size: 10pt; mso-no-proof: yes;">}</span><o:p></o:p></p>
</div>
<p>&nbsp;</p>
<p>Such programs are generated, compiled and the query template creation method is dynamically invoked to register a new query template in StreamInsight. This approach has some limitations:</p>
<ul>
<li>Compiler error reporting may or may not be useful. I use #line pragmas in the generated code to pinpoint the location of any local errors in the user&rsquo;s query, but the LINQ query definition may not be a well-formed expression. Consider what would happen if you were to call <strong>scripter.CreateQueryTemplate(app, &ldquo;q&rdquo;, null, &ldquo;input1; return null }&rdquo;);</strong> </li>
<li>Related to the above point, there is an injection risk. <strong>The usual common sense security rules apply: do not evaluate code from untrusted sources.</strong> </li>
<li>Every call to <strong>CreateQueryTemplate</strong> results in the creation of a new assembly that remains loaded for the lifetime of the app domain. </li>
</ul>
<p>Full source code for the scripter is attached. The event definition code is intentionally extensible to support specification of stream event types using something other than <strong>System.Type</strong>. Possible alternatives: <strong>CepEventType</strong> or some other representation of the event record layout.</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/10/05/65/14/ScriptingStreamInsight.zip" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-05-65-14/ScriptingStreamInsight.zip">ScriptingStreamInsight.zip</a></p>]]></content:encoded>
    </item>
    <item>
      <title>EF Extensions for Visual Studio 2010</title>
      <link>https://docs.microsoft.com/archive/blogs/meek/ef-extensions-for-visual-studio-2010</link>
      <pubDate>Thu, 19 Aug 2010 08:32:26 GMT</pubDate>
      <dc:creator><![CDATA[Colin Meek]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/meek/2010/08/19/ef-extensions-for-visual-studio-2010/</guid>
      <description><![CDATA[The EF Extensions sample has been updated for Visual Studio 2010. It’s available for download here....]]></description>
      <content:encoded><![CDATA[<p>The EF Extensions sample has been updated for Visual Studio 2010. It’s available for download <a href="https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=EFExtensions&amp;ReleaseId=3624" target="_blank">here</a>. Nothing new in this release… some features have actually been removed because they’re no longer needed in .NET 4.0.</p>  <ul>   <li>The EF proper now includes an <a href="http://msdn.microsoft.com/en-us/library/dd412719(VS.100).aspx" target="_blank">ObjectSet&lt;TEntity&gt;</a> class that makes the old EntitySet&lt;TEntity&gt; EFExtensions class redundant. The FindOrAttach and GetTrackedEntities methods from the original EFExtensions sample are preserved as extension methods over ObjectSet, but the EntitySet class is no more. A cheat sheet for people upgrading from EntitySet to ObjectSet (just some name changes): </li> </ul>  <table border="1" cellspacing="0" cellpadding="2" width="430"><tbody>     <tr>       <td valign="top" width="225"><strong>Before</strong></td>        <td valign="top" width="203"><strong>After</strong></td>     </tr>      <tr>       <td valign="top" width="225">Microsoft.Data.Extensions.EntitySet&lt;T&gt;</td>        <td valign="top" width="203">System.Data.Objects.ObjectSet&lt;T&gt;</td>     </tr>      <tr>       <td valign="top" width="225">InsertOnSaveChanges(T)</td>        <td valign="top" width="203">AddObject(T)</td>     </tr>      <tr>       <td valign="top" width="225">DeleteOnSaveChanges(T)</td>        <td valign="top" width="203">DeleteObject(T)</td>     </tr>      <tr>       <td valign="top" width="225">FindOrAttach(T)</td>        <td valign="top" width="203"><em>extension method in DataExtensions</em></td>     </tr>      <tr>       <td valign="top" width="225">GetTrackedEntities(T)</td>        <td valign="top" width="203"><em>extension method in DataExtensions</em></td>     </tr>      <tr>       <td valign="top" width="225">Metadata</td>        <td valign="top" width="203">EntitySet</td>     </tr>   </tbody></table>  <ul>   <li>The Zip helper method is redundant with System.Linq.Enumerable.Zip. </li>    <li>The LINQ ExpressionVisitor class is now public, so Microsoft.Data.Extensions.ExpressionVisitor has been replaced with System.Linq.Expressions.ExpressionVisitor. </li> </ul>  <p><em>Quick aside on ExpressionVisitor: a couple of devs have mentioned a “bug” to me in the shipping ExpressionVisitor implementation. It’s actually a bug <u>fix</u> relative to the visitor code that has circulated via various blogs (including this one). The VisitMemberInit() method now visits the MemberInitExpression.NewExpression expression but requires that the returned expression is a NewExpression. This may cause trouble for partial evaluator implementations that unwittingly assume that, for instance, ‘new Foo { X = … }’ can be rewritten as ‘Constant(new Foo()) { X = … }’.</em></p>  <p>Although the EF now supports richer mapping of stored procedures and includes some ‘drill-through-to-the-underlying-store-provider’ surface, a few remaining gaps allowed Materializer, CreateStoreCommand and company to survive the chopping block in this release of EFExtensions. Some remaining benefits of the sample library: multiple result sets from stored procedures, the power of a complete programming language to describe result transformations, and support for arbitrary result shapes.</p>  <p>Thanks,    <br />-C</p>]]></content:encoded>
    </item>
  </channel>
</rss>