6

I have a column in a SQL database table of JSON data. Can I use Entity Framework and LINQ to query and filter based on a field/value in the SQL JSON column?

I am using SQL 2016, VS 2017, EF Core 2.0, .NET Core 2.0.

3
  • See this answer: stackoverflow.com/a/36396525/1258525 Commented Aug 27, 2017 at 0:40
  • This article states that Sql Server can query JSON value column but LINQ cannot: codeproject.com/Articles/1166099/… Commented Aug 27, 2017 at 0:42
  • Brian, the reference on Code Project was helpful. Because my JSON can actually be different for each row, just addressing this with a T-SQL using .FromSql (leverage SQL JSON_VALUE) seems like the best choice. Commented Aug 27, 2017 at 13:12

1 Answer 1

9

Brian provides some links to a few good options. I think these all still require you to fully receive the SQL data, then apply filtering in .NET code; I'd really like to filter on the SQL server side of things and avoid pulling back all the rows, then filtering.

In addition, because my JSON data can have different properties for each row, filtering with SQL Server would be best.

I'm going to opt for using a SQL statement in LINQ per Brian's Code Project reference:

var blogs = _context.Blogs
  .FromSql<Blog>(@"SELECT * FROM Blogs WHERE JSON_VALUE(Owner, '$.Name') = {0}", Owner)
  .ToList();
Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way to check if an item exists in array for JSON data in SQL?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.