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 inputStream.TumblingWindow(
- TimeSpan.FromSeconds(5),
- HoppingWindowOutputPolicy.ClipToWindowEnd)
- select new
- {
- SensorId = sensorGroups.Key,
- Sum = window.Sum(e => e.Value)
- };
Running this throws the error:
|
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)
- };
Comments
- Anonymous
November 04, 2010
Mark.These two pieces of code are the exact same aren't they?. Should the first code have said"from window in inputStream.TumblingWindow" - Anonymous
November 04, 2010
Mark.These two pieces of code are the exact same aren't they?. Should the first code have said"from window in inputStream.TumblingWindow" - Anonymous
November 05, 2010
doh; cut and paste strikes again. Thanks for the catch!