I have
dbContext.Items.FromSql("SELECT COUNT(*)
FROM Items
WHERE JSON_VALUE(Column, '$.json') = 'abc'")
This returns an IQueryable
, I am wondering how I can return a scalar int
back?
I have
dbContext.Items.FromSql("SELECT COUNT(*)
FROM Items
WHERE JSON_VALUE(Column, '$.json') = 'abc'")
This returns an IQueryable
, I am wondering how I can return a scalar int
back?
You can do something like:
dbContext.Items.Count()
You always can do a .Count()
Function on an IQueryable
Edit: When the need of a FromSql is really there something like this should do the job:
var count = context.Items.FromSql("Select count(*) from items").First();
FromSQL has some limitations:
So, try this:
var elements = dbContext.Items
.FromSql("SELECT * from dbo.Items")
.ToList();
var countOfElements = elements.Count();
the fastest hack/workaround is if your Item class has an int/long property (lets say Id) you can treat it like this:
dbContext.Items.FromSql("SELECT COUNT(*) as Id
FROM Items
WHERE JSON_VALUE(Column, '$.json') = 'abc'").Select(x=>x.Id).First();
var count = dbContext.Set.FromSqlRaw(/* {raw SQL} */).Count();
Will generate the following SQL
SELECT COUNT(*)::INT
FROM (
-- {raw SQL}
) AS c
where {raw SQL}
is of the form
select count(*) from my_table where my_col_condition = true group by my_col_id
The count work can then perfectly be done on the database-side this way, without loading table rows on the client.
Be careful not to end {raw SQL}
with ;
.
You should pass composable
SELECT
SQL toFromSql
method, e.g.SELECT *
- see Raw SQL Queries. Then you can apply regular LINQQueryable
operators, includingCount
: