In SQL Server 2016+, I have a table that has a JSON field that contains an array of Ids. (note I have told OrmLite to store in JSON not JSV)
I would like to do a query server side using the new JSON functions to find a match, whereas currently now I am pulling the entire dataset onto the server and then doing a Contains with Linq.
var q = db.From<Media>();
q.Where($$"{q.Column<Media>(x => x.RegionId)} IN (SELECT value from OPENJSON({q.Column<Media>(x => x.Regions, tablePrefix:true)}, '$$'))");
var results = db.Select(q.Select("*"));
This makes use of any naming conventions or aliases on the properties, if you don’t have any you can use nameof instead:
var q = db.From<Media>()
.Where($$"{nameof(Media.RegionId)} IN (SELECT value from OPENJSON({nameof(Media)}.{nameof(Media.Regions}}, '$$'))")
.Select("*");
var results = db.Select(q);