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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Nov 15, 2012, 6:25 AM EST
David SmithRank #999 · 2K reputationPosted Nov 15, 2012, 8:05 AM EST
David SmithRank #999 · 2K reputationPosted Nov 15, 2012, 8:26 AM EST
SELECT tableExample.ServiceType, Count(tableExample.FilledByID) AS CountOfFilledByID
FROM tableExample
GROUP BY tableExample.ServiceType
HAVING tableExample.ServiceType<>"ST-Service"
VulpesRank #55 · 29.2K reputationPosted Nov 15, 2012, 9:22 AM EST
David SmithRank #999 · 2K reputationPosted Nov 15, 2012, 9:29 AM EST
CountOfFilledByID = g.Count(te => te.FilledByID != null)};
VulpesRank #55 · 29.2K reputationPosted Nov 15, 2012, 9:35 AM EST
If it can never be null, just use:
David SmithRank #999 · 2K reputationPosted Nov 15, 2012, 9:48 AM EST
CountOfFilledByID = g.Count(te => te.FilledByID > 0)};
David SmithRank #999 · 2K reputationPosted Nov 15, 2012, 10:00 AM EST
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};
VulpesRank #55 · 29.2K reputationPosted Nov 15, 2012, 10:26 AM EST
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;