How to write this sql query in Linq below
SELECT tableExample.FilledByID, tableExample.ServiceType
FROM tableExample
GROUP BY tableExample.FilledByID, tableExample.ServiceType
HAVING (((tableExample.ServiceType)<>"ST-Service"));
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Nov 15, 2012, 3:26 PM
CountOfFilledByID = g.Count(te => te.FilledByID > 0)
which is not the same as Count(te.FilledByID) in SQL.
The latter will only exclude NULL values, not zero or negative values.
Regarding the next part of the query, you only need to use an anonymous type if there's more than one value to be returned - otherwise just use a 'scalar' value.
Also, you have a WHERE clause here rather than a HAVING clause so it needs to come before the grouping takes place.
I'd therefore suggest:
var query= from te in table where te.ServiceType == "CS-Transportation" || te.ServiceType == "CS-Shopping" group te by te.FilledByID into g select g.Key;
David SmithPosted Nov 15, 2012, 3:00 PM
SELECT te.FilledByID
FROM te
WHERE (te.ServiceType)="CS-Transportation" Or (te.ServiceType)="CS-Shopping
GROUP BY te.FilledByID;
so far i have this below, I am trying to incorporate the where clause above in blue in the linq below.
var query= from te in table
group te by new { te.FilledById }
into g
select new { Num = g.Key.FilledById};
David SmithPosted Nov 15, 2012, 2:48 PM
CountOfFilledByID = g.Count(te => te.FilledByID > 0)};
VulpesPosted Nov 15, 2012, 2:35 PM
If it can never be null, just use:
David SmithPosted Nov 15, 2012, 2:29 PM
CountOfFilledByID = g.Count(te => te.FilledByID != null)};
VulpesPosted Nov 15, 2012, 2:22 PM
David SmithPosted Nov 15, 2012, 1:26 PM
SELECT tableExample.ServiceType, Count(tableExample.FilledByID) AS CountOfFilledByID
FROM tableExample
GROUP BY tableExample.ServiceType
HAVING tableExample.ServiceType<>"ST-Service"
David SmithPosted Nov 15, 2012, 1:05 PM
VulpesPosted Nov 15, 2012, 11:25 AM