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
- // This query calculates the sum of all sensor values
- //for each sensor
- //for each 5 seconds worth of data.
- var query = from e in inputStream
- group e by e.SensorId into sensorGroups
- from window in sensorGroups.TumblingWindow(
- TimeSpan.FromSeconds(5),
- HoppingWindowOutputPolicy.ClipToWindowEnd)
- select new
- {
- SensorId = sensorGroups.Key,
- Sum = window.Sum(e => e.Value)
- };
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
- var query = from e in inputStream
- group e by e.SensorId into sensorGroups
- from window in sensorGroups.TumblingWindow(
- TimeSpan.FromSeconds(5),
- HoppingWindowOutputPolicy.ClipToWindowEnd)
- select new
- {
- SensorId = sensorGroups.Key,
- Sum = window.Sum(e => e.Value)
- };