使用LINQ重构到筛选列表

使用LINQ重构到筛选列表,linq,list,find,Linq,List,Find,我使用了几个相似对象的列表。任何对象都具有列表属性。基本上,我需要比较几个列表 我本以为LINQ是实现这一点的理想方法,但在尝试了连接、扩展方法、使用收益率等之后,我仍然遇到了麻烦 对于使用LINQ重构我的代码有什么建议吗 更新:我正在重构ContieneServidor(ContainsServer-translate)方法 私有静态bool contieneservidorfactoring(列出datosgruposservicedores,字符串nombremakina) { var总计

我使用了几个相似对象的列表。任何对象都具有列表属性。基本上,我需要比较几个列表

我本以为LINQ是实现这一点的理想方法,但在尝试了连接、扩展方法、使用收益率等之后,我仍然遇到了麻烦

对于使用LINQ重构我的代码有什么建议吗

更新:我正在重构ContieneServidor(ContainsServer-translate)方法

私有静态bool contieneservidorfactoring(列出datosgruposservicedores,字符串nombremakina)
{
var总计=Datosgruposervidores
.SelectMany(g=>g.Servidores)
其中(x=>x.Nombre.Equals(nombreMaquina)).Count();
如果(总数>0)返回true;
返回false;
}
我的代码:

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;
}
var datosgruposervidores=new List();
var gruposCompletos=新列表();
var maquinasSeleccionadas=新列表();
...
//Máquinas公司
var maquinasnocontradas=新列表();
foreach(gruposCompletos中的变量g)
{
foreach(g.Servidores中的var服务器)
{
var encontrado=
ContieneServidor(datosgruposervidores,server.Nombre);
如果(!encontrado)maquinasnocontradas.Add(server.Nombre);
}
}
foreach(maquinasSeleccionadas中的m变量)
{
var encontrado=ContieneServidor(datosgruposervidores,m);
如果(!encontrado)maquinasnocontradas.Add(m);
}
如果(MAQUINASNOECONTRADAS.Count>0)
{
var sb=新的StringBuilder();
var sep=“”;
foreach(MAQUINASNOECONTRADAS中的var maq)
{
sb.追加(sep+maq);
sep=“,”;
}
System.Diagnostics.Trace.WriteLine(“Máquinas no encontradas:+sb.ToString());
抛出新的InvalidOperationException(“Máquinas no encontradas:+sb.ToString());
}
}
专用静态布尔控制器(
列表Datosgruposervidores,字符串nombremakina)
{
foreach(datosgruposervidores中的变量g)
{
var servidor=g.Servidores.其中(s=>s.Nombre.Equals(nombreMaquina));
if(servidor!=null&&servidor.Count()>0)返回true;
}
返回false;
}
私有静态bool contieneservidorfactoring(列出datosgruposservicedores,字符串nombremakina)
{
var总计=Datosgruposervidores
.SelectMany(g=>g.Servidores)
其中(x=>x.Nombre.Equals(nombreMaquina)).Count();
如果(总数>0)返回true;
返回false;
}
类型:

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; }
}
公共类GrupoServidorDto
{
public int IdGrupo{get;set;}
公共字符串Nombre{get;set;}
私有列表服务商=新列表();
公开名单服务商
{
获取{返回服务商;}
设置{servidores=value;}
}
}
公共级服务
{
公共int Id{get;set;}
公共字符串Nombre{get;set;}
公共字符串IP{get;set;}
公共字符串Entorno{get;set;}
公共字符串习惯化{get;set;}
公共字符串Tipo{get;set;}
public int IdGrupo{get;set;}
}
[可序列化()]
公共类gruposervisorseleccionado
{
[XmlAttribute()]
public int IdGrupo{get;set;}
[XmlAttribute()]
公共字符串Nombre{get;set;}
私人名单服务商=
新列表();
[XmlElement()]
公开名单服务商
{
获取{返回服务商;}
设置{servidores=value;}
}
[XmlAttribute()]
公共bool estantodasamaquinasseleccionadas{get;set;}
公共GruPoServiceOrselectionado(){}
}
[可序列化()]
公共类服务电子商务
{
[XmlAttribute()]
公共int Id{get;set;}
[XmlAttribute()]
公共字符串Nombre{get;set;}
[XmlAttribute()]
公共字符串IP{get;set;}
[XmlAttribute()]
公共字符串Entorno{get;set;}
[XmlAttribute()]/[XmlIgnore()]
公共字符串习惯化{get;set;}
[XmlAttribute()]
公共字符串Tipo{get;set;}
[XmlAttribute()]
public int IdGrupo{get;set;}
}
我想您需要:

var maquinasNoEncontradas = gruposCompletos
                   .SelectMany(g => g.Servidores)
                   .Select(x => x.Nombre)
                   .Concat(maquinasSeleccionadas)
                   .Where(x => !ContieneServidor(datosGruposServidores, x))
                   .ToList();
然后:

if (maquinasNoEncontradas.Count > 0)
{
    // This assumes .NET 4; it's *slightly* more awkward in .NET 3.5, but
    // still simpler than doing it by hand.
    string text = string.Join(", ", maquinasNoEncontradas);

    System.Diagnostics.Trace.WriteLine("Máquinas no encontradas: " + text);
    throw new InvalidOperationException("Máquinas no encontradas: " + text);
}
您可能会构建逗号分隔的版本,然后测试它是否为空字符串。。。但老实说,我不确定我会不会。我想你想要:

var maquinasNoEncontradas = gruposCompletos
                   .SelectMany(g => g.Servidores)
                   .Select(x => x.Nombre)
                   .Concat(maquinasSeleccionadas)
                   .Where(x => !ContieneServidor(datosGruposServidores, x))
                   .ToList();
        string.Join(", ", gruposCompletos
                              .SelectMany(x => x.Servidores)
                              .Select(x => x.Nombre)
                              .Concat(maquinasSeleccionadas)
                              .Where(x => !ContieneServidor(datosGruposServidores, x))
                              .ToArray());
然后:

if (maquinasNoEncontradas.Count > 0)
{
    // This assumes .NET 4; it's *slightly* more awkward in .NET 3.5, but
    // still simpler than doing it by hand.
    string text = string.Join(", ", maquinasNoEncontradas);

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

您可能会构建逗号分隔的版本,然后测试它是否为空字符串。。。但老实说,我不确定我会不会。你忽略了
maquinasSeleccionadas
是一个
列表。我不敢否决你,但我相信你会改正的it@smartcaveman:修好了,谢谢。(当变量名对我来说没有意义时,我发现阅读代码要困难得多。当然,这不是抱怨——我相信它们对OP的用处比英文的要大!)你忽略了
maquinasSeleccionadas
是一个
列表。我不敢否决你,但我相信你会改正的it@smartcaveman:修好了,谢谢。(当变量名对我来说没有意义时,我发现读代码要困难得多。当然,这不是抱怨——我相信它们对OP的用处比英文的要大!)
        string.Join(", ", gruposCompletos
                              .SelectMany(x => x.Servidores)
                              .Select(x => x.Nombre)
                              .Concat(maquinasSeleccionadas)
                              .Where(x => !ContieneServidor(datosGruposServidores, x))
                              .ToArray());