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.

JsonContains and sub-properties

See original GitHub issue

Is there a support for querying by a sub-property of an object within a collection? For example, I have the following classes defined

public class MyEntity
{
    [Column("Info", TypeName = "jsonb")]
    public Info Info { get; set; }
}

public class Info
{
    public List<Tag> Tags { get; set; }
}

public class Tag
{
   public int TagId { get; set; }
   public int OtherData { get; set; }
}

How do I find all the MyEntity rows that have at least one Tag with TagId = 10 ? Here’s what I’ve tried so far:

// didn't work
var result = db.MyEntities.Where(m => EF.Functions.JsonContains(m.Info.Tags.Select(t => t.TagId), "10") ).ToList();

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
rojicommented, Apr 20, 2022

@grcd that checks whether the entire Tags sub-document exists as provided, so it checks whether there’s only one tag with ID 10 (any additional tag will cause this to return false).

0reactions
grcdcommented, Apr 19, 2022

Is there a support for querying by a sub-property of an object within a collection? For example, I have the following classes defined

public class MyEntity
{
    [Column("Info", TypeName = "jsonb")]
    public Info Info { get; set; }
}

public class Info
{
    public List<Tag> Tags { get; set; }
}

public class Tag
{
   public int TagId { get; set; }
   public int OtherData { get; set; }
}

How do I find all the MyEntity rows that have at least one Tag with TagId = 10 ? Here’s what I’ve tried so far:

// didn't work
var result = db.MyEntities.Where(m => EF.Functions.JsonContains(m.Info.Tags.Select(t => t.TagId), "10") ).ToList();

What if:

var result = db.MyEntities.Where(m => EF.Functions.JsonContains(m.Info, @"{""Tags"": {""TagId"": ""10""}}")).ToList();

?

Read more comments on GitHub >

github_iconTop Results From Across the Web

MySQL use JSON_CONTAINS with a subquery
You could just concat the double quotes in the subquery: SELECT * FROM addresses WHERE JSON_CONTAINS( `groups`, (SELECT CONCAT('"', u.group, ...
Read more >
How to unnest / extract nested JSON data in MySQL 8.0
For DB users who work with JSON string in MySQL 8.0, a guide on how to unnest, flatten, and extract nested JSON data...
Read more >
How to read nested JSON files and convert to case class ...
First, we need to understand our data structure. File climate-fever.json contains three properties (key-value pairs), animal_claims, ...
Read more >
Use MySQL JSON field in Laravel - QCode
JSON_CONTAINS () function accepts the JSON field being searched and another to compare against. It returns 1 when a match is found, e.g....
Read more >
JSON nested objects
JSON nested objects ... Objects can be nested inside other objects. Each nested object must have a unique access path. The same field...
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