question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamic select from jsonb

See original GitHub issue

Hi,

I’m creating dynamic system and some values are stored in jsonb in Postgres. I would like to filter values from json by selecting like this:

select values->>name, values->>description from cms.contents c where c.type = 'product'

with linq query:

db.Contents.Select("name", "description") because input is List<string>

How can I do that in linq2db? I tried something like SqlKata but I don’t want change my ORM because of a few jsonb columns. 😦

Thanks! Pavel

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Nov 9, 2020

Yes, it is possible via dynamic expressions. Try to produce expression tree dynamically like this one:

Expression<Func<Context, SomeResult>> mapper = 
   c => new SomeResult
   { 
       Id = c.Id,
       Values = new Dictionary<string, string> { 
           {"name", c.Values.JsonProp("name")},
           {"description", c.Values.JsonProp("description")},
       }
   }
1reaction
sdanylivcommented, Nov 9, 2020

Still cannot handle whole idea

[Sql.Expression("{0}->>{1}", ServerSideOnly = true)]
public static string JsonProp(this JObject obj, [SqlQueryDependent] string propName)
   => throw new NotImplementedException();

db.GetTable<Content>()
   .Where(c => c.Values.JsonProp("name") == someName)
   .Select(c => new { name.= c.Values.JsonProp("name"), description = c.Values.JsonProp("description")})

For making projection whole dynamic, I think yes you have to create class dynamically. I need to check one solution, maybe we can convert JObject to Dictionary in projection automatically

Read more comments on GitHub >

github_iconTop Results From Across the Web

Postgres jsonb query for dynamic values - sql
Find rows containing a key in a JSONB array of records. Generate query dynamically. SELECT 'SELECT * FROM users WHERE experience @? '...
Read more >
Can I select keys stored in a postgres jsonb column as ...
But what I'm curious is if I can somehow tell PG I want all keys in the JSONB column to be selected as...
Read more >
Postgres jsonb query for dynamic values - DevPress官方社区
Find rows containing a key in a JSONB array of records. Generate query dynamically. SELECT 'SELECT * FROM users WHERE experience @? '...
Read more >
Passing dynamic column names an data in json to query with
From above json i want to dynamically filter my table where col1 = 'test' and col2... This can be completly dynamic so there...
Read more >
How to get value from JSON with dynamic key in Postgres
Basically, getting all key names that contain 'foo' from a column called 'objects'. SELECT keys FROM ( SELECT jsonb_object_keys(objects) as keys ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found