Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

179
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda