Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
Count the number of element present in the inner list
public Class Users
{
    string UserName,
    List<UserDesktop> userDesktop
}

public class UserDesktop
{
    int ID,
    string DesktopName,
    string type -- here Type value is A, B;
}

Here I have to find the users count having desktop assigned of type A Users variable containing all data.

List<Users> finalUsers = users.Count(s=>s.userDesktop.where(x=>x.type == "A"))

Please let me know the correct way of doing it.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You might want to use SelectMany to flatten the list then count it:

int numberOfUsersWithDesktopOfTypeA = users.SelectMany(u => u.userDesktop)
                                           .Count(ud => ud.type == "A");

If you want the list of users that have an A desktop:

var usersWithDesktopA = Users.Where(u => u.Any(ud => ud.type == "A"));
var numberOfUsersWithDesktopA = usersWithDesktopA.Count();
over 4 years ago · Santiago Trujillo Report

0

To query the user(s) who has UserDesktop of type 'A':

List<Users> hasDesktopAUsers = users.Where(u => u.userDesktop.Any(ud => ud.type == "A"))
        .ToList();

Next, you can get the number of count from the previous result as:

int numberOfDesktopAUsers = hasDesktopAUsers.Count;

Out of topic:

It is recommended to have a proper naming convention for handling single and plural terms to avoid confusion. For example:

public class User
{
    public string UserName { get; set; }
    public List<UserDesktop> UserDesktops { get; set; }
}

public class UserDesktop
{
    public int ID { get; set; }
    public string DesktopName { get; set; }
    public string Type { get; set; }
}

From the above, you should name as User as it is an User object while UserDesktops with 's' as it is a collection set.

Final solution:

List<User> hasDesktopAUsers = users.Where(u => u.UserDesktops.Any(ud => ud.Type == "A"))
    .ToList(); 
int numberOfDesktopAUsers = hasDesktopAUsers.Count;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!