How do I cound the number of items in a List
List
Perhaps something similar to -> liststCarColors.FindAll("Blue")?
How do I cound the number of items in a List
List
Perhaps something similar to -> liststCarColors.FindAll("Blue")?
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 attend4days leftRegister
Aravind GovindarajRank #430 · 5.8K reputationPosted Feb 20, 2024, 2:05 PM EST
If you are doing wildcard matching, then please use this snippet
int count = itemList.Count(item => item.Contains("blue"));
if you are looking exact match then please follow the below snippet
int count = itemList.Count(item => item == "blue");
Jignesh KumarRank #91 · 22.9K reputationPosted Feb 20, 2024, 11:07 PM EST
Please check this one,
Add linq reference,
using System.Linq;
List liststCarColors = new List { "red", "blue", "green", "blue", "yellow", "blue" }; var count = liststCarColors.Count(x => x.Contains("blue"));Snigdha Rath70 reputationPosted Feb 21, 2024, 12:49 AM EST
You can count the number of items in a List that contain the word "blue" using LINQ's
Countmethod along with a lambda expression. For example:using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { List listCarColors = new List() { "blue", "green", "blueish", "dark blue", "lightblue", "red" }; int count = listCarColors.Count(color => color.Contains("blue")); Console.WriteLine($"Number of items containing 'blue': {count}"); } }Cr BhargaviRank #420 · 5.9K reputationPosted Feb 21, 2024, 1:24 AM EST
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { List carColors = new List { "blue car", "red car", "blue sky", "green car" }; int count = carColors.Count(color => color.Contains("blue")); Console.WriteLine(count); // This will output the count of items containing the word "blue" } }