Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to have a proxy property for json/jsonb field for LINQ-query purposes? #3164

Open
eSPiYa opened this issue May 9, 2024 · 1 comment

Comments

@eSPiYa
Copy link

eSPiYa commented May 9, 2024

This json data field is unstable but with some fields that seems exists in all instances. These json data were extracted from a third-party service tool so we don't have any control on it. I want to preserve the original data while having a proxy property just for LINQ-querying because using JsonDocument mapping is quite tedious, some fields that I need are from great grandchild:
var records = this.dbSet.AsQueryable().Where(r => r.Data.RootElement.GetProperty("Properties").GetProperty("MetaData").GetProperty("Provider").GetProperty("Name").GetString() == "AWS").ToList();

I tried to create an unmapped property as proxy(MappedData) by ignoring it in fluent API(I wrote these codes by hand because my work laptop got limited internet connection):
class Log {
public Guid Id {get;set;}
public DateOnly Date {get;set;}
public JsonDocument Data {get;set;
public MappedJsonData MappedData{
get => JsonSerializer.Deserialize(this.Data);
set => this.Data = JsonSerializer.SerializeToDocument(value);
}
}

It translates fine to the result, but not if used in filtering like in Where clause:
var records = this.dbSet.AsQueryable().Where(r => r.MappedData.Properties.MetaData.Provider.Name == "AWS").ToList();
I'm getting an error related to MappedData that it can't get translated because it is unmapped.

I tried to use Newtonsoft's JSON.NET because it seems easier to perform LINQ queries and not that tedious. Even after I applied the extension UseJsonNet() and replaced the type of Data to JObject.

Thanks!

@roji
Copy link
Member

roji commented May 11, 2024

I'm not sure I follow what you're trying to do... Do you want to use your "proxy property" to somehow express queries over your real column in the database? Do you want the structure of the proxy property to somehow be nicer than your actual data? How would EF know what actual SQL query to generate?

A full, clearer code sample showing what you're trying could help here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants