# Dynamically Create Query in using asp dotnet client

**URL:** <https://discuss.elastic.co/t/dynamically-create-query-in-using-asp-dotnet-client/360167>\
**Category:** Elastic Search\
**Created:** [May 24, 2024, 2:06pm UTC](https://discuss.elastic.co/t/dynamically-create-query-in-using-asp-dotnet-client/360167 "2024-05-24T14:06:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tifflynn](https://avatars.discourse-cdn.com/v4/letter/t/e9a140/32.png) [@tifflynn](https://discuss.elastic.co/u/tifflynn)\
**Post date:** [May 24, 2024, 2:06pm UTC](https://discuss.elastic.co/t/dynamically-create-query-in-using-asp-dotnet-client/360167/1 "2024-05-24T14:06:09Z")

</div>

The following is a query written in C#. It leverages Elastic.Clients.Elasticsearch version 8.13.7. This is NOT the NEST client.

> <https://gist.github.com/tiflynn/76696413d1e81c87bf2071ce592d17fd>

The query uses `myTenants` which is passed in as a parameter to the method. The type of `myTenants` is `IEnumerable<Tuple<string, string>>`. Each item in the list represents and ORI/CRI pair. I need to update the query to traverse the entire `myTenants` list. The query should return all `SubmissionDto` where the ORI/CRI combination is equal to those in `myTenants`

I've tried a bunch of different queries cant seem to get it to work

---

<div class="post-metadata">

**Author:** ![Artem\_Shelkovnikov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/artem_shelkovnikov/32/134322_2.png) [@Artem\_Shelkovnikov](https://discuss.elastic.co/u/Artem_Shelkovnikov)\
**Post date:** [May 27, 2024, 1:59pm UTC](https://discuss.elastic.co/t/dynamically-create-query-in-using-asp-dotnet-client/360167/2 "2024-05-27T13:59:48Z")

</div>

Hi Tiffany,

I'm not an expert with .NET client but I'll try to help. I have a couple questions:

1. `myTenants.ElementAt(0).Item1.ToLower()` and `myTenants.ElementAt(0).Item2.ToLower()` are static values, right?
2. Have you checked the actual queries that are generated by the client?

IMO the best approach to figure out what the problem is would be to make a query in Kibana Dev tools and then try to use it via the client, debugging which query is created by the client itself. [This guide](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/debug-information.html) might be helpful to see the query generated by the client.

---

<div class="post-metadata">

**Author:** ![tifflynn](https://avatars.discourse-cdn.com/v4/letter/t/e9a140/32.png) [@tifflynn](https://discuss.elastic.co/u/tifflynn)\
**Post date:** [May 28, 2024, 4:04pm UTC](https://discuss.elastic.co/t/dynamically-create-query-in-using-asp-dotnet-client/360167/3 "2024-05-28T16:04:08Z")

</div>

Im not sure what you mean by if they are static values. I was just doing some testing to make sure the query worked with 1 ori/cri pair. The issues is i need multiple ori/cri pairs but I am not sure how to construct that query dynamically ( based on the array ) in c#. I am using the kibana tool to debug also. THanks for the resposne

---

<div class="post-metadata">

**Author:** ![Artem\_Shelkovnikov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/artem_shelkovnikov/32/134322_2.png) [@Artem\_Shelkovnikov](https://discuss.elastic.co/u/Artem_Shelkovnikov)\
**Post date:** [May 28, 2024, 4:42pm UTC](https://discuss.elastic.co/t/dynamically-create-query-in-using-asp-dotnet-client/360167/4 "2024-05-28T16:42:22Z")

</div>

> Im not sure what you mean by if they are static values

Consider the following examples using SQL:

```auto
SELECT * FROM Customers WHERE language_code = "en_us"

```

The query above uses static value while filtering - "en\_us".

Then another query:

```auto
SELECT * FROM Customers WHERE home_phone = mobile_phone

```

This query does not use static values - it queries the data based on the data itself.

Queries with static values are easy and should be achievable using regular query statements that you have - `must`, `filter` and similar.

If you want to make a query that returns results based on the filter that checks values within document (like in the example - if customer has same phone number specified for home and mobile) you will need to do it via scripting like here: [Accessing document fields and special variables | Elasticsearch Guide [8.13] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-fields.html)

---

<div class="post-metadata">

**Author:** ![tifflynn](https://avatars.discourse-cdn.com/v4/letter/t/e9a140/32.png) [@tifflynn](https://discuss.elastic.co/u/tifflynn)\
**Post date:** [May 28, 2024, 4:59pm UTC](https://discuss.elastic.co/t/dynamically-create-query-in-using-asp-dotnet-client/360167/5 "2024-05-28T16:59:52Z")

</div>

oh ok i misunderstood static in this context. No the values are not static. This was me just making sure the query worked when there was just 1 ori/cri pair. I am trying to achieve the following logic

```auto
(Ori = A && Cri = B) || (Ori = C && Cri = D) || ...

```

So the query needs to be dynamically built based on what is in `myTenants`

---

<div class="post-metadata">

**Author:** ![Artem\_Shelkovnikov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/artem_shelkovnikov/32/134322_2.png) [@Artem\_Shelkovnikov](https://discuss.elastic.co/u/Artem_Shelkovnikov)\
**Post date:** [May 29, 2024, 8:24am UTC](https://discuss.elastic.co/t/dynamically-create-query-in-using-asp-dotnet-client/360167/6 "2024-05-29T08:24:45Z")

</div>

I see. Indeed it should be possible with the approach that you're taking. Have you had a chance to build a query yourself in Kibana Dev Tools and compare to the query generated by the client?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 26, 2024, 8:25am UTC](https://discuss.elastic.co/t/dynamically-create-query-in-using-asp-dotnet-client/360167/7 "2024-06-26T08:25:31Z")

</div>

Closed on June 26, 2024, 8:25am UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
