Can Entity Framework (LINQ) select rows based on JSON where clause? - sql

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.

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();

Related

Reverse engineer a cube(Analysis Service) enabling data access for .NET applications?

how to reverse engineer an SQL cube(Analysis Service)so that a data access for .NET applications is enabled , just like Entity Framework (ex. dataBase first approach ) but instead of an SQL DB a live Cube(Multidimensional Expressions) that can be queried via LINQ query ?
Use a data provider such as ADOMD which can connect to a cube to read its structure, and run MDX queries to return tables of values.

update cosmosDB azure sql

I'm trying to update a value of document created on cosmosDB azure using sql api. The problem is that the requests update/ delete/ Insert don't work.
client.queryDocuments(
collectionUrl,
UPDATE tableC
SET prev = 12
WHERE condition
).toArray((err, results) => {
if (err) res.json({ 'A': 12 });
else {
res.json({ 'A': 15});
}
})
CosmosDB SQL is not ANSI SQL implementation. It supports just querying in a somewhat similar manner, but it's not the same thing.
From Introduction to Azure Cosmos DB: SQL API:
Azure Cosmos DB supports querying documents using a SQL language,
which is rooted in the JavaScript type system, and expressions with
support for relational, hierarchical, and spatial queries. The Azure
Cosmos DB query language is a simple yet powerful interface to query
JSON documents. The language supports a subset of ANSI SQL grammar
and adds deep integration of JavaScript object, arrays, object
construction, and function invocation.
So basically CosmosDB takes some syntax rules and conventions from SQL but when you look closer it's another beast. Ansi SQL contains things CosmosDB SQL API does not have and cosmosDB SQL has things ANSI SQL does not have.
Why?
Not having support for entire ANSI SQL makes sense, as SQL was designed for relational data manipluation needs. CosmosDB is not a relational database and its change model is working with documents as the changeable units, not individual fields or sets. In DoucmentDB you add one entire document, update one entire document, or delete one entire document. As long as this stands, it does not need the complexity of traditional SQL insert/update/delete syntax.
CosmosDB's flavor of SQL only supports querying.
You cannot use data manipulation SQL.

How can I use Dapper in a multi database environment?

I'm trying to implement simple queries like SELECT * FROM TABLE_X WHERE XID = #id, but the problem that I'm having is that these queries would run on different databases (SQL Server and Oracle) for different application instances.
How to do it without to have to write each database a new set of queries?
Dapper is really closed to the database, and allow you to leverage pure sql tricks specific for a specific database. In my opinion you should use a query object pattern, so you will have an interface in front of each extraction /commit that would possibly change for SQL/Oracle.
I've downloaded the code of SqlMapper.cs and hacked SetupCommand to check if the command is from Oracle or SQL Server.
That was what I did:
if (cnn.GetType().Name.ToLowerInvariant().Contains("oracle"))
{
sql = sql.Replace('#', ':');
}

Do linq generated queries get cached effectively by SQL Server 2008?

Do linq generated queries get cached effectively by SQL Server 2008?
or is it better to use stored procedures with linq or what about a view and then using compiled linq queries... opinions?
cheers
emphasis here is on "effectively", and or is it better....
ie. views are cached well by sql server, and then using linq on the view....
On top of the answers already given according to Damien Guard there's a glitch in the LINQ to SQL and EF LINQ providers that fails to set the variable lengths consistently for queries involving string parameters.
http://damieng.com/blog/2009/12/13/sql-server-query-plan-cache
Apparently it's fixed in .NET 4.0.
In the past I've written stored proc's in place of LINQ queries, mainly for complex reporting-like queries rather than simple CRUD but only following profiling of my application.
L2S simply passes queries on to SQL Server 2008. So they will get cached, or not cached, like any other query submitted by any other process. The fact that a Linq query is compiled has no impact on how SQL Server processes the query.
Queries that LINQ generates are normal SQL queries like your own hand-crafted SQL queries, and they follow the same rules: if the query text is identical (down to the last comma and whitespace) than a query text before, chances are its query execution plan might have been cached and thus able to be reused.
The point is: the query text has to be absolutely identical - if even a single whitespace is different, SQL Server considers it a new query and thus will go through the full process of parsing, analysing, finding a query plan and executing it.
But basically, yes - queries sent off by LINQ will be cached and reused - if they meet those criteria!

SQL to LINQ generator

I am new to LINQ and just wanna know; is there any application in which I type standard SQL and it gives me its representing statement in linq?
Previous SO on similar topic:
SQL to LINQ Tool