StreamInsight: Obscure LINQ error - Stream other than apply input stream is cannot be referenced inside apply branch

Another little LINQ error you might encounter from time to time.  Ran into this yesterday while building out some queries, and figured it was worth a quick post.  Starting with a basic stream, I needed to group by a set of fields in the stream and calculate some basic aggregates.

Code Snippet

  1. // This query calculates the sum of all sensor values
  2. // for each sensor
  3. // for each 5 seconds worth of data.            
  4. var query = from e in inputStream
  5.     group e by e.SensorId into sensorGroups
  6.             from window in inputStream.TumblingWindow(
  7.         TimeSpan.FromSeconds(5),
  8.         HoppingWindowOutputPolicy.ClipToWindowEnd)
  9.     select new
  10.     {
  11.         SensorId = sensorGroups.Key,
  12.         Sum = window.Sum(e => e.Value)
  13.     };

Running this throws the error:

 Microsoft.ComplexEventProcessing.Linq.QueryGenerationException was unhandled by user code  Message=Stream other than apply input stream is cannot be referenced inside apply branch. The following expression is not supported: 'sensorGroups => CreateAdapterStream("input", StreamInsight.Samples.Adapters.SimpleTextFileReader.TextFileReaderFactory, value(StreamInsight.Samples.Adapters.SimpleTextFileReader.TextFileReaderConfig), Point, value(Microsoft.ComplexEventProcessing.Linq.CepStreamCreationContext)).TumblingWindow(FromSeconds(5), HoppingWindowOutputPolicy.ClipToWindowEnd)'.

See the subtle yet annoyingly obvious after the fact mistake I made?  I grouped by sensorGroups, but windowed over inputStream.  Fix this to use the same stream for the window and the group resolves the error.

Code Snippet

  1. var query = from e in inputStream
  2.     group e by e.SensorId into sensorGroups
  3.             from window in sensorGroups.TumblingWindow(
  4.         TimeSpan.FromSeconds(5),
  5.         HoppingWindowOutputPolicy.ClipToWindowEnd)
  6.     select new
  7.     {
  8.         SensorId = sensorGroups.Key,
  9.         Sum = window.Sum(e => e.Value)
  10.     };