I am trying to convert an SQL statement into a LAMBDA expression. I have a basic understanding, I know how to write simple statements c/w filter portion(WHERE) but could not find anything on how to add conditionals into the SELECT portion.
Right now I query without the conditional part but the web server must loop through millions of returned records to do the calculation where I would prefer the SQL server to perform this on the first call.
Anyway I am not asking for the complete solution, I am looking for a examples that have aggregate functions with a conditional expressions, ... if that is even possible.
Here is one of the sql statement I need to convert.
Thank you in advance for any input /info on the matter.
P:oD
Right now I query without the conditional part but the web server must loop through millions of returned records to do the calculation where I would prefer the SQL server to perform this on the first call.
Code:
var EDsAndCounts = db.Electors. Where(x => x.VotedStrikeList == true || x.VotedPollBook == true). Select(x => x).ToList();
Here is one of the sql statement I need to convert.
Thank you in advance for any input /info on the matter.
P:oD
Code:
SELECT elector.ed_code, Sum(CASE WHEN votedstrikelist = 1 THEN 1 ELSE 0 END) AS VotedStrikeListCount, Sum(CASE WHEN votedpollbook = 1 THEN 1 ELSE 0 END) AS VotedPollBookCount, Sum(CASE WHEN votedpollbook = 1 OR votedstrikelist = 1 THEN 1 ELSE 0 END) AS TotalCount FROM elector WHERE ( votedstrikelist = 1 ) OR ( votedpollbook = 1 ) GROUP BY elector.ed_code
Comment