# c# API - Is it possible to build (dynamically) and run aggregate  queries to  receive real time streaming results using the c# API

**URL:** <https://forum.kx.com/t/c-api-is-it-possible-to-build-dynamically-and-run-aggregate-queries-to-receive-real-time-streaming-results-using-the-c-api/13719>\
**Category:** Community Support\
**Tags:** imported, other\
**Created:** [June 12, 2023, 12:00am UTC](https://forum.kx.com/t/c-api-is-it-possible-to-build-dynamically-and-run-aggregate-queries-to-receive-real-time-streaming-results-using-the-c-api/13719 "2023-06-12T00:00:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dg1](https://avatars.discourse-cdn.com/v4/letter/d/d07c76/32.png) [@dg1](https://forum.kx.com/u/dg1)\
**Post date:** [June 12, 2023, 12:00am UTC](https://forum.kx.com/t/c-api-is-it-possible-to-build-dynamically-and-run-aggregate-queries-to-receive-real-time-streaming-results-using-the-c-api/13719/1 "2023-06-12T00:00:00Z")

</div>

[https://learninghub.kx.com/forums/topic/c-api-is-it-possible-to-build-dynamically-and-run-aggregate-queries-to-receive-real-time-streaming-results-using-the-c-api](https://learninghub.kx.com/forums/topic/c-api-is-it-possible-to-build-dynamically-and-run-aggregate-queries-to-receive-real-time-streaming-results-using-the-c-api)

I’m dynamically building and executing aggregate queries vs kdb using the c# API. However I have to periodically poll/re execute the query for updates.

I would like to build and run aggregate queries from c# so that I receive real time aggregated results. Is this possible? If so please provide a brief explanation and references to particular docs.

Thanks  
DG

---

<div class="post-metadata">

**Author:** ![lkerr2](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/lkerr2/32/57_2.png) [@lkerr2](https://forum.kx.com/u/lkerr2)\
**Post date:** [June 13, 2023, 12:00am UTC](https://forum.kx.com/t/c-api-is-it-possible-to-build-dynamically-and-run-aggregate-queries-to-receive-real-time-streaming-results-using-the-c-api/13719/2 "2023-06-13T00:00:00Z")

</div>

Hi DG,

Here are a few solutions depending on requirements/specifics:

1. Depending on how _dynamic_ the queries are, i.e. if it’s a mostly fixed list of aggregations, I would suggest using a Real Time Subscriber (RTS) and creating tables for the different aggregations, then subscribing to those aggregate tables from C# using **.u.sub**

- [Q Real Time Subscriber](https://code.kx.com/q/wp/rt-tick/#real-time-vwap-subscriber")

- [C# KDB Q Subscriber Example](https://github.com/KxSystems/csharpkdb/blob/main/Demos/SubscriberDemo/Program.cs")

1. If the aggregations are very dynamic, create on the C# side a polling query to Q. This would be similar to static queries (examples below) but wrapped in a looping statement within C#.

- [AquaQ Documentation on C# Aggregations](https://dataintellect.com/wp-content/uploads/2015/01/AquaQ-Analytics-C-to-Kdb+-API-document-V1.0.pdf")
- [KX Example Simple Query](https://github.com/KxSystems/csharpkdb/blob/main/Demos/QueryResponseDemo/Program.cs")

Note this second solution wouldn’t be truly Real-Time, but you could reduce the polling time to an extent where it’s close enough to real-time for your purposes.

1. If you’re truly looking for Real-Time responses you could subscribe via a Gateway Process and treat it as an aggregator, then define the aggregation that’s run on-update via remote queries from C#. Something along the lines of:

connection.ks(“.my.agg:{select avg price from x}”)

Where the string would be defined by C# dynamically and run on-change.

The 3rd method is quite a bit more complicated than the other two and will have a few issues that need to be overcome. I have noted it to answer this question in it’s completion but would advise either approach 1 or 2. If a non-polling approach is definitely required I’ll be happy to go into more detail on the third solution and the _gotchas_.

Regards,

Sam B
