Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4238496
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:54:45+00:00 2026-05-21T02:54:45+00:00

I use several lists of similar objects. Anyone object has a List property. Basically,

  • 0

I use several lists of similar objects. Anyone object has a List property. Basically, I need compare several lists.

I would have thought that LINQ would be an ideal way of doing this but after trying joins, extension methods, using yields, etc. I’m still having trouble.

any suggestions for refactor my code to using LINQ ?

Update: I’m refactor ContieneServidor (ContainsServer translate) method

 private static bool ContieneServidorRefactoring(List<GrupoServidorDto> datosGruposServidores, string nombreMaquina)
    {
            var total = datosGruposServidores
                 .SelectMany(g => g.Servidores)
                 .Where(x => x.Nombre.Equals(nombreMaquina)).Count();
            if (total > 0) return true;
            return false;
    }

My code:

var datosGruposServidores = new List<GrupoServidorDto>();    
var gruposCompletos = new List<GrupoServidorSeleccionado>();
var maquinasSeleccionadas = new List<string>();
...

// Comprobación de Máquinas
var maquinasNoEncontradas = new List<string>();
foreach (var g in gruposCompletos)
{
    foreach (var server in g.Servidores)
    {
        var encontrado = 
            ContieneServidor(datosGruposServidores, server.Nombre);
        if (!encontrado) maquinasNoEncontradas.Add(server.Nombre);
    }
}

foreach (var m in maquinasSeleccionadas)
{
    var encontrado = ContieneServidor(datosGruposServidores, m);
    if (!encontrado) maquinasNoEncontradas.Add(m);
}

if (maquinasNoEncontradas.Count > 0)
{
    var sb = new StringBuilder();
    var sep = "";
    foreach (var maq in maquinasNoEncontradas)
    {
        sb.Append(sep + maq);
        sep = ", ";
    }

    System.Diagnostics.Trace.WriteLine("Máquinas no encontradas: " + sb.ToString());
    throw new InvalidOperationException("Máquinas no encontradas: " + sb.ToString());
}

}

private static bool ContieneServidor(
    List<GrupoServidorDto> datosGruposServidores, string nombreMaquina)
{
    foreach (var g in datosGruposServidores)
    {
        var servidor = g.Servidores.Where(s => s.Nombre.Equals(nombreMaquina));
        if (servidor != null && servidor.Count() > 0) return true;
    }
    return false;
}

private static bool ContieneServidorRefactoring(List<GrupoServidorDto> datosGruposServidores, string nombreMaquina)
{
        var total = datosGruposServidores
             .SelectMany(g => g.Servidores)
             .Where(x => x.Nombre.Equals(nombreMaquina)).Count();
        if (total > 0) return true;
        return false;
}

The types:

public class GrupoServidorDto
{
    public int IdGrupo { get; set; }

    public string Nombre { get; set; }

    private List<ServidorDto> servidores = new List<ServidorDto>();

    public List<ServidorDto> Servidores
    {
        get { return servidores; }
        set { servidores = value; }
    }
}


public class ServidorDto
{
    public int Id { get; set; }

    public string Nombre { get; set; }

    public string IP { get; set; }

    public string Entorno { get; set; }

    public string Habilitado { get; set; }

    public string Tipo { get; set; }

    public int IdGrupo { get; set; }
}


[Serializable()]
public class GrupoServidorSeleccionado
{
    [XmlAttribute()]
    public int IdGrupo { get; set; }

    [XmlAttribute()]
    public string Nombre { get; set; }

    private List<ServidorSeleccionado> servidores = 
        new List<ServidorSeleccionado>();

    [XmlElement()]
    public List<ServidorSeleccionado> Servidores
    {
        get { return servidores; }
        set { servidores = value; }
    }

    [XmlAttribute()]
    public bool EstanTodasLasMaquinasSeleccionadas { get; set; }

    public GrupoServidorSeleccionado() { }
}


[Serializable()]
public class ServidorSeleccionado
{
    [XmlAttribute()]
    public int Id { get; set; }

    [XmlAttribute()]
    public string Nombre { get; set; }

    [XmlAttribute()]
    public string IP { get; set; }

    [XmlAttribute()]
    public string Entorno { get; set; }

    [XmlAttribute()] // [XmlIgnore()]
    public string Habilitado { get; set; }

    [XmlAttribute()]
    public string Tipo { get; set; }

    [XmlAttribute()]
    public int IdGrupo { get; set; }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T02:54:45+00:00Added an answer on May 21, 2026 at 2:54 am
            string.Join(", ", gruposCompletos
                                  .SelectMany(x => x.Servidores)
                                  .Select(x => x.Nombre)
                                  .Concat(maquinasSeleccionadas)
                                  .Where(x => !ContieneServidor(datosGruposServidores, x))
                                  .ToArray());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object (Ticket), which has a list of other objects (Message). Message
When I need to add several identical items to the list I use list.extend:
We have several jobs that run concurrently that have to use the same config
I have several functions that I wrote and I use regularly on my servers,
I have several blocks of the following code that each use there own matrix.
While trying to use LINQ to SQL I encountered several problems. I have table
My question is about memory use and objects in actionscript 2. If I have
I know tat similar questions has been asked already several times. And i do
Basically NUnit, xUnit, MbUnit, MsTest and the like have methods similar to the following:
I have a single page jQuery mobile application that consists of several list views

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.