Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

182
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda