How to count the number of elements that match a condition with LINQ
int divisor = AllMyControls.Where(p => p.IsActiveUserControlChecked).Count()
or simply
int divisor = AllMyControls.Count(p => p.IsActiveUserControlChecked);
Since you are a beginner, it would be worthwhile to take a look at Enumerable
documentation
Count elements with a condition in a sublist with linq
_eventManager.QueryEvents().Where(x => x.StartTime > DateTime.Today)
.SelectMany(x => x.EventUsers)
.Count(z => z.UserId == userId && !z.HasResponded)
Linq Get count from multiple where condition
var result = records.GroupBy(x => x.Date)
.Select(x => new { Date = x.Key, Count = x.Count() });
is what you want
result will now have a list of dates and count.
if you want a specific value then like this:
var result = records.Where(x => x.Record == -1).
.GroupBy(x => x.Date)
.Select(x => new { Date = x.Key, Count = x.Count() });
To group by the date and not the date time use the date property of the object in the GroupBy statement
.GroupBy(x => x.Date.Date)
How can i use Count with condition in LINQ
You need to pass a lambda that takes the entity and returns a bool
, like this
var id = Session["CustomerID"];
var recordcount = db.EventParticipants.Where(x => x.CustomerID == id).Count();
Note you'll also want to put the id into a separate variable as EF will not be able to translate Session
into SQL.
Linq Query with a where condition - count items
How about this:
query = query.Where(item => context.Table
.Count(t => t.amount == item.Amount) >= 10);
Or to reduce the number of round-trips:
var counts = context.Table
.GroupBy(t => t.amount)
.Select(g => new {amount = g.Key, count = g.Count()});
query = from q in query
join c in counts
on q.amount equals c.amount
where c.count >= 10
select q;
LinQ query that counts the number of elements in a list of objects in another list of objects
if you select each family, then return the count of males in it, you would get an enumerable of counts - e.g {0,1,0,3,2,1} - you can then simply Sum those counts
ListOfFams.Select(f => f.persons.Where(p => p.gender == gender.male).Count()).Sum();
//to reject nulls
ListOfFams.
Where(f => f != null && f.persons != null).
Select(f => f.persons.
Where(p => p != null && p.gender == gender.male).
Count()).
Sum();
Linq count condition in query
Perhaps the LINQ Count
function is called for:
var query = (from u in users
where u.Roles.Any(r => r.RoleId == role.Id)
from t in u.ProjectTasks.Where(x => x.Users.Any(user => user.Id == u.Id)).DefaultIfEmpty()
where ((u.ProjectTasks.Count() == 0) || u.ProjectTasks.Count(z => z.Status == Status.Active || z.Status == Status.Testing) <= 3)
select new { User = u } into Users
group Usersby Users.User).ToList();
For examples of usages: http://www.csharp-examples.net/linq-count/
count objects that meet certain condition in List-collection
There's is a count method using a predicate : see Enumerable.Count Method (IEnumerable, Func)
Note that this method is an extension method and you can use it only if you add a reference to the System.Linq namespace.
Related Topics
How to Simulate a Mouse Click at a Certain Position on the Screen
Contextswitchdeadlock Was Detected Error in C#
Restoring Window Size/Position with Multiple Monitors
Find and Return JSON Differences Using Newtonsoft in C#
How to Test Your Request.Querystring[] Variables
How to Connect and Use a SQLite Database from C#
Thread Safe Collections in .Net
Fastest Way to Remove Duplicate Value from a List<> by Lambda
Truncate String on Whole Words in .Net C#
How to Use Acrylic Accent in Windows 10 Creators Update
Rsa Encryption, Getting Bad Length
What Is the Default Value for Enum Variable
Overriding Gethashcode for Mutable Objects
Automatic Cookie Handling C#/.Net Httpwebrequest+Httpwebresponse