473,386 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Lambda with SUM+Conditional

pod
298 100+
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.

Expand|Select|Wrap|Line Numbers
  1. var EDsAndCounts = db.Electors.
  2.                 Where(x => x.VotedStrikeList == true
  3.                     || x.VotedPollBook == true).
  4.                 Select(x => x).ToList();
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

Expand|Select|Wrap|Line Numbers
  1. SELECT elector.ed_code, 
  2.        Sum(CASE 
  3.              WHEN votedstrikelist = 1 THEN 1 
  4.              ELSE 0 
  5.            END) AS VotedStrikeListCount, 
  6.        Sum(CASE 
  7.              WHEN votedpollbook = 1 THEN 1 
  8.              ELSE 0 
  9.            END) AS VotedPollBookCount, 
  10.        Sum(CASE 
  11.              WHEN votedpollbook = 1 
  12.                    OR votedstrikelist = 1 THEN 1 
  13.              ELSE 0 
  14.            END) AS TotalCount 
  15. FROM   elector 
  16. WHERE  ( votedstrikelist = 1 ) 
  17.         OR ( votedpollbook = 1 )
  18. GROUP  BY elector.ed_code 
  19.  
Feb 2 '16 #1
2 3314
PsychoCoder
465 Expert Mod 256MB
Might now be exactly what you're looking for but try using the Contains conditional expression, similar to this

Expand|Select|Wrap|Line Numbers
  1. var EDsAndCounts = db.Electors.
  2.                 Where(x => x.VotedStrikeList == true
  3.                     || x.VotedPollBook == true || x.Contains("Criteria").
  4.                 Select(x => x).ToList();
  5.  
Mar 1 '16 #2
pod
298 100+
Thank you PsychoCoder for your reply... but that is not what exactly what I needed, I wanted the SUM of individual cases, and I figured out a way to do it ... I decided to do it outside the query

Expand|Select|Wrap|Line Numbers
  1.             var myList = db.Electors.
  2.                             Where(x => x.VotedStrikeList == true
  3.                                 || x.VotedPollBook == true
  4.                                 || x.Flagged == true
  5.                                 || x.Comment.Length > 0).
  6.                             Select(x => x).ToList();
  7.             ViewBag.VSL = myList.Sum(c => (c.VotedStrikeList == true ? 1 : 0));
  8.             ViewBag.VPB = myList.Sum(c => (c.VotedPollBook == true ? 1 : 0));
  9.             ViewBag.FLG = myList.Sum(c => (c.Flagged == true ? 1 : 0));
  10.             ViewBag.COM = myList.Sum(c => (c.Comment.Length > 0 ? 1 : 0));
  11.             ViewBag.PRC = myList.Sum(b => (b.VotedStrikeList == true ? 1 :
  12.                                             (b.VotedPollBook == true ? 1 :
  13.                                                   (b.Flagged == true ? 1 :
  14.                                                (b.Comment.Length > 0 ? 1 : 0)))));
May 31 '16 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

53
by: Oliver Fromme | last post by:
Hi, I'm trying to write a Python function that parses an expression and builds a function tree from it (recursively). During parsing, lambda functions for the the terms and sub-expressions...
63
by: Stephen Thorne | last post by:
Hi guys, I'm a little worried about the expected disappearance of lambda in python3000. I've had my brain badly broken by functional programming in the past, and I would hate to see things...
26
by: Steven Bethard | last post by:
I thought it might be useful to put the recent lambda threads into perspective a bit. I was wondering what lambda gets used for in "real" code, so I grepped my Python Lib directory. Here are some...
181
by: Tom Anderson | last post by:
Comrades, During our current discussion of the fate of functional constructs in python, someone brought up Guido's bull on the matter: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 ...
25
by: Russell | last post by:
I want my code to be Python 3000 compliant, and hear that lambda is being eliminated. The problem is that I want to partially bind an existing function with a value "foo" that isn't known until...
4
by: Xah Lee | last post by:
A Lambda Logo Tour (and why LISP languages using λ as logo should not be looked upon kindly) Xah Lee, 2002-02 Dear lispers, The lambda character λ, always struck a awe in me, as with...
23
by: Kaz Kylheku | last post by:
I've been reading the recent cross-posted flamewar, and read Guido's article where he posits that embedding multi-line lambdas in expressions is an unsolvable puzzle. So for the last 15 minutes...
5
by: Octal | last post by:
How does the lambda library actually works. How does it know how to evaluate _1, how does it recognize _1 as a placeholder, how does it then calculate _1+_2, or _1+2 etc. The source files seem a...
21
by: globalrev | last post by:
i have a rough understanding of lambda but so far only have found use for it once(in tkinter when passing lambda as an argument i could circumvent some tricky stuff). what is the point of the...
1
by: Tim H | last post by:
Compiling with g++ 4: This line: if_then_else_return(_1 == 0, 64, _1) When called with a bignum class as an argument yields: /usr/include/boost/lambda/if.hpp: In member function 'RET...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.