Select objects associated with lists related to LINQ

0

I explain my scenario.

  • I have a method that returns a list of objects Cuenta ( Id_Cta , Desc_Cta ) in the GetCuentas(filtro) method
  • I have a method that returns a list of objects Usuario ( Id_Usuario , CodUsuario ) in the GetUsuarios() method
  • I have a method that returns a list of objects that relate accounts with their users ( Id_Cta , Id_Usuario ) in the GetUsuariosCtas() method
  • What I need is a LINQ statement to obtain a list of users from the list of accounts obtained with GetCuentas(filtro)

    EDIT: The classes would look like this:

    public class Cuenta_poco { public int Id_Cta {get; set;} public string Desc_Cta  {get; set;} }
    
    public class Usuario_poco { public int Id_Usuario {get; set;} public string Desc_Usuario  {get; set;} }
    
    public class RelUsuarioCta_poco { public int Id_Cta {get; set;} public int Id_Usuario  {get; set;} }
    
    public class Cuenta() { public List<Cuenta_poco> GetCuentas(filtro) {//Devuelve lista } }
    
    public class Usuario() { public List<Usuario_poco> GetUsuarios() {//Devuelve lista } }
    
    public class RelUsuarioCta() { public List<RelUsuarioCta_poco> GetRelaciones() {//Devuelve lista } }
    
        
    asked by Carlos 09.01.2017 в 14:51
    source

    2 answers

    0

    I've got a solution. Starting from the base that we have the necessary objects correctly instantiated:

    Cuenta cuenta = new Cuenta();
    RelUsuarioCta usuarioCuenta = new RelUsuarioCta();
    Usuario usuario = new Usuario();
    
    var resultmp = usuariocuenta.GetRelaciones().Where(r => cuenta.GetCuentas(filtro).Any(c => c.Id_Cta == r.Id_Cta)).ToList();                 
    var usuarios = usuario.GetUsuarios().Where(u => resultmp.Any(r => r.Id_Usuario == u.Id_Usuario)).ToList();
    

    My intention was to do it in a single sentence, but if I combine these two sentences in one, the performance drops dramatically.

    I have also tried doing it in a single LINQ statement using sub selects, but the performance is also horrible, it takes a long time to return the results compared to the solution I propose.

    If someone can provide an optimal solution that works with a single sentence, I would appreciate it.

    Greetings.

        
    answered by 10.01.2017 / 10:16
    source
    0

    From the list returned by the GetCuentas() method, you could obtain a list of accounts that meet some condition or any property of the Cuenta_poco object or some anonymous type created from these properties, for example

    var filteredlist = accounts_list.Where (c = & ct.Id_Cta > 1);

    var query = from u in list_account where u.Id_Cta == 1 select u;

    To create a list of type User_no you should make a query between the sequences returned by the methods GetCuentas and GetRelaciones .

    Note the code you place has errors and does not compile:

      using System;
        using System.Collections.Generic;
        using System.Linq;
    
        public class Cuenta_poco
        {
            public int Id_Cta { get; set; }
            public string Desc_Cta { get; set; }
            }
    
        public class Usuario_poco
        {
            public int Id_Usuario { get; set; }
            public string Desc_Usuario { get; set; }
        }
    
        public class RelUsuarioCta_poco
        {
            public int Id_Cta { get; set; }
            public int Id_Usuario { get; set; }
        }
    
        public class Cuenta
            {
            // Especificar el tipo del filtro . Se sugiere usar el delegado Predicate<T>
                public List<Cuenta_poco> GetCuentas()
                {
                    return new List<Cuenta_poco>
                 {
                     new Cuenta_poco{ Id_Cta = 1 , Desc_Cta="Descripcion cuenta 1 " },
                     new Cuenta_poco{ Id_Cta = 2 , Desc_Cta="Descripcion cuenta 2 " },
                     new Cuenta_poco{ Id_Cta = 3 , Desc_Cta="Descripcion cuenta 3 " },
                 };
                }
            }
    
            public class Usuario
            {
                public List<Usuario_poco> GetUsuarios()
                {
                    return new List<Usuario_poco>
                 {
             new Usuario_poco{ Id_Usuario = 1 , Desc_Usuario= "Descripcion usurario 1 "},
             new Usuario_poco{ Id_Usuario = 2 , Desc_Usuario= "Descripcion usurario 2 " },
             new Usuario_poco{ Id_Usuario = 3 , Desc_Usuario= "Descripcion usurario 3 "},
                };
                }
            }
    
        public class RelUsuarioCta
        {
            public List<RelUsuarioCta_poco> GetRelaciones()
            {
                //Devuelve lista 
    
                return new List<RelUsuarioCta_poco>
             {
                 new RelUsuarioCta_poco{ Id_Cta = 1 , Id_Usuario= 3 },
                 new RelUsuarioCta_poco{ Id_Cta = 2 , Id_Usuario= 2 },
                 new RelUsuarioCta_poco{ Id_Cta = 3 , Id_Usuario= 1 },
            };
            }
        }
    
        public class Program
        {
            static void Main()
            {
                Cuenta cuenta = new Cuenta();
                // 1. Obtenemos los datos
                List<Cuenta_poco> lista_cuentas = cuenta.GetCuentas();
                // 2. Creamos la consulta usando metodos de extension
                IEnumerable<Cuenta_poco> listafiltrada = lista_cuentas.Where(c => c.Id_Cta > 1);
                // 3. Ejecutamos la consulta
                // Si solo requieres la lista deberias de llamar al metpdo .ToList() y cambiar el tipo de listafiltrada
                foreach (Cuenta_poco u in listafiltrada)
                    Console.WriteLine($"Id_Cta :{u.Id_Cta}  Desc_Cta :{u.Desc_Cta}");
    
    
                //2. Creamos la consulta usando la sintansis de consulta    
    
        IEnumerable<Cuenta_poco> consulta =
                  from u in lista_cuentas
                  where u.Id_Cta == 1
                  select u;
    
                // 3. Ejecutamos la consulta 
                // Si solo requieres la lista deberias de llamar al metpdo .ToList();
                foreach (Cuenta_poco u in consulta)
                    Console.WriteLine($"Id_Cta :{u.Id_Cta}  Desc_Cta :{u.Desc_Cta}");
            }
    
        }
    
        
    answered by 10.01.2017 в 00:28