---
title: "Linq Count based on Boolean"  
description: "Linq Count based on Boolean"  
author: "Anonymous User"  
published: 2013-06-11  
updated: 2013-06-12  
canonical: https://www.mindstick.com/forum/1040/linq-count-based-on-boolean  
category: "vb.net"  
tags: ["vb.net"]  
reading_time: 1 minute  

---

# Linq Count based on Boolean

Hi [Developers](https://www.mindstick.com/articles/12875/blunders-you-must-avoid-while-hiring-java-developers-to-get-the-best-fulfilling-your-needs), \
I'm trying to get a [linq query](https://www.mindstick.com/forum/33908/how-to-select-and-retrieve-data-using-linq-query-in-entity-framework) running which I can't get right.\
I have a list of a [custom](https://www.mindstick.com/blog/12298/use-custom-writing-services-to-get-through-the-finals) class called Occurrences, this class has two [properties](https://www.mindstick.com/articles/23331/nootropics-7-different-types-and-their-unique-properties) Code as [String](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) and Negative as Boolean. I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to get the net total of Code (group by Code) so this would be a count of Code where Negative = False (All positive), subtract a count of Code where Negative = True (All negative). There is no quantity in the Occurrences class, each occurrence counts as 1 negative or positive.\
I tried to do this in 3 separate [queries](https://www.mindstick.com/forum/33678/sub-queries-in-sql-server) which did not work, ideally I would like to do this in 1 query.\
Please let me know if you need a better [explanation](https://yourviews.mindstick.com/view/84608/what-are-hindenburg-s-allegations-against-adani-detailed-explanation) or if I am unclear.\
Edit: Sample input/[output](https://www.mindstick.com/interview/34427/explain-the-output-in-angular)\
Input:\
Code Negative-------------------123 True123 True123 False456 True456 True456 True789 False789 FalseOutput:\
Code Count----------------123 -1456 -3789 +2\
Any help on above is really appreciated.

## Replies

### Reply by AVADHESH PATEL

Hi,\
You can try as following\
from item in data group item by item.Code into g\
**select new { Code = g.Key, Count = g.Sum(x => x.Negative ? -1 : 1) }**


---

Original Source: https://www.mindstick.com/forum/1040/linq-count-based-on-boolean

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
