sql - Count non null values in multiple columns with LINQ -
the following sql query counts non-null values of multiple columns in single query (as in this answer):
select count(id) total, count(column_1) column_1_non_null_count, count(column_2) column_2_non_null_count, count(column_3) column_3_non_null_count, ... mytable
is there corresponding linq query executes sql query similar 1 (without subquery each column count)?
counting null values instead of non-null values ok.
i'm not sure exist way entity framework, think better raw sql.
but assuming want entity framework, may 1 way creating several queries using futurecount method ef.extended library. using future methods ef.extended, queries postponed until accessed result of 1 of queries , data retrieved in 1 round trip database server.
var querycolumn1 = mydbcontext.mytable.where(q => q.column1 == null).futurecount(); var querycolumn2 = mydbcontext.mytable.where(q => q.column2 == null).futurecount(); ... int countcolumn1 = querycolumn1.value; int countcolumn2 = querycolumn2.value
what dislike of solution readibility of code, said think approach using raw sql or using stored procedured
Comments
Post a Comment