efcore.pg icon indicating copy to clipboard operation
efcore.pg copied to clipboard

Querying json data which contains array of objects with Linq

Open senabd opened this issue 6 years ago • 3 comments

Hi,

I have a user table which contains data column type and json data column type.

Like this: Firstname, Lastname, JsonData

JsonData column's format like this:

"User1" : {
    "a": [
        {
            "b": "Value of b0",
            "c": "Value of c0"
        },
        {
            "b": "Value of b1",
            "c": "Value of c1"
        }
    ]
}

"User2" : {
    "a": [
        {
            "b": "Value of b2",
            "c": "Value of c2"
        },
        {
            "b": "Value of b3",
            "c": "Value of c3"
        }
    ]
}

I would like to querying users table with Linq. The query should return users which "b" contains "Value of b0"

Example query:

Users.Where(x=>x.a.Where(y=>y.b == "Value of b2"));

Expected result: Row of User1

Npgsql version: 3.1.4 PostgreSQL version: PostgreSQL 11.4 Operating system: Windows 10

Other details about my project setup:

Dotnet Core 3.1 Project, EF Core version: 3.1.5

senabd avatar Aug 11 '20 12:08 senabd

@senkadir have you tried JSON mapping as described in the docs page? If you've encountered issues, can you please post a minimal code sample and the error you're getting?

roji avatar Aug 11 '20 13:08 roji

Hi,

We used POCO Mapping as described in the docs page.

My model like this:

public class User
{
   public Guid Id {get;set;}
   public string FirstName {get;set;}
   public JsonModel JsonData {get;set;}
}

public class JsonModel
{
   public List<Data> Data=new List<Data>; 
}

public class Data
{
   public string b {get;set;}
   public string c {get;set;}
}

Our linq query is:

List<User> datas = await _uow.AsQueryable<User>() .Where(x => (EF.Functions.?(“Which method and how should we use in POCO mapping”)) ).ToListAsync();

We just try to get users that includes “value of b” in b property, however we didn’t get any error when we try in the docs page but we are getting empty result.

senabd avatar Aug 12 '20 13:08 senabd

List isn't as fully-supported as plain arrays - try using that. Below is a sample which works for me for accessing the first element of an array.

Sample code
class Program
{
    static async Task Main(string[] args)
    {
        await using var ctx = new BlogContext();
        await ctx.Database.EnsureDeletedAsync();
        await ctx.Database.EnsureCreatedAsync();

        var datas = await ctx.Users
            .Where(x => x.JsonData.SomeArray[0].b == "SomeValue").ToListAsync();
    }
}

public class BlogContext : DbContext
{
    public DbSet<User> Users { get; set; }

    static ILoggerFactory ContextLoggerFactory
        => LoggerFactory.Create(b => b.AddConsole().AddFilter("", LogLevel.Information));

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseNpgsql(@"Host=localhost;Username=test;Password=test")
            .EnableSensitiveDataLogging()
            .UseLoggerFactory(ContextLoggerFactory);
}

public class User
{
    public Guid Id { get; set; }
    public string FirstName { get; set; }
    [Column(TypeName = "jsonb")]
    public JsonModel JsonData { get; set; }
}

public class JsonModel
{
    public Data[] SomeArray { get; set; }
    public int SomeProperty { get; set; }
}

public class Data
{
    public string b {get;set;}
    public string c {get;set;}
}

roji avatar Aug 14 '20 12:08 roji