Relate two C # dictionaries

5

I have two text files with the following data (separated by tabs):

CLIENT

ID_CLIENTE  CLIENTE MERCADO
1198        CTA           G
1285        FGF           B
1112        EJESA         B
1131        ESPERANZA     F
1150        NORP          B
1141        LUQUE         B
1151        TUCU          F
1235        BERNV         G

SPENDING

FECHA           ID_CLIENTE  GASTO
01/08/2017      1198        25647   
01/08/2017      1285        4885
01/08/2017      1112        71987
01/08/2017      1131        1000
01/08/2017      1150        5
01/08/2017      1141        98
01/08/2017      1151        84
01/08/2017      1235        5899
02/08/2017      1198        257 
02/08/2017      1285        4885
02/08/2017      1112        1987
02/08/2017      1131        1489
02/08/2017      1150        5489
02/08/2017      1141        8
02/08/2017      1151        841
02/08/2017      1235        589
03/08/2017      1198        257 
03/08/2017      1285        4
03/08/2017      1112        1987
03/08/2017      1131        1589
03/08/2017      1150        2489
03/08/2017      1141        89
03/08/2017      1151        841
03/08/2017      1235        589

Now create a class for each file and individualize the data.

public class Cliente
    {
        public int IdCliente { get; set; }
        public string Nombre { get; set; }
        public string Mercado { get; set; }
    }


public class Consumo
    {
        public int IdCliente { get; set; }
        public DateTime Fecha { get; set; }
        public int Gasto { get; set; }
    }

What I need to do is take the maximum cost of each client and print it in a text file. I have to do it with Dictionaries.

Statement:

List<Cliente> Clientes;
List<Consumo> Consumos;

Dictionary<int, Cliente> dClientes;
Dictionary<int, List<Consumo>> dConsumos;

#region Constructor

public Reporte(List<Cliente> Clientes, List<Consumo> Consumos)
{
    this.Clientes = Clientes;
    this.Consumos = Consumos;
}

#endregion

Loading data to Dictionaries:

 dClientes = Clientes.ToDictionary(c => c.IdCliente, v => v);


        dConsumos = Consumos.Where(c => dClientes.Keys.Contains(c.IdCliente)).
            GroupBy(c => c.IdCliente).
            ToDictionary(c => c.Key, v => v.ToList<Consumo>());

My question is: How I relate (INNER JOIN to ID_CLIENTE) both dictionaries to be able to perform this operation (greater expense) or what kind of methods are used in these cases. If you can perform this operation in a separate method and then implement it would be better.

EXPECTED DEPARTURE

CLIENTE    MERCADO  MAXIMO GASTO
CTA           G          2547
FGF           B          4885
EJESA         B          71987
ESPERANZA     F          1589
NORP          B          5489
LUQUE         B          98
TUCU          F          841
BERNV         G          5899

Greetings, any questions or answers to the question I am available.

    
asked by byte96 04.04.2018 в 01:08
source

1 answer

7

To do what you ask, you must use the SelectMany :

  

Project each element of a sequence into an IEnumerable and reduce the resulting sequences in a sequence.

The query is a bit complex, and it will be necessary to group twice per IdCliente since it may be the case that the maximum expenditure of a client is repeated. More or less it would be something like this:

var dConsumos = Consumos.Where(c => dClientes.Keys.Contains(c.IdCliente))
                        .GroupBy(x => x.IdCliente)
                        .SelectMany(b => b.Where(c => c.Gasto == b.Max(d => d.Gasto)))
                        .GroupBy(x => x.IdCliente)
                        .ToDictionary(c => c.Key, v => v.ToList<Consumo>());

There may be some easier way to do it, but I think it works correctly.

Edit

To print the data obtained but also showing fields that are in Clientes you have several options. Here I put a very simple without having to touch the query:

foreach (var maximo in dConsumos)
{
    Console.WriteLine("{0}\t{1}\t{2}", dClientes[maximo.Key].NombreCliente, dClientes[maximo.Key].Mercado, maximo.Value.First().Gasto);
}
    
answered by 04.04.2018 / 10:03
source