List c # help list distribution

1

I am new to the list development, I have a query: Assuming I have a list with fields id, name, store, code. and the code is repeated for example 001,002,003,001,001 how to divide the list into 2. one for the repeated codes and another for the non-repeated ...

    
asked by ELopez 26.11.2018 в 02:51
source

1 answer

1

You could use the% lin_% co to know which ones are repeated

var result = from item in dbcontext.NombreTabla
              group item by item.codigo into g
              select new {
                 codigo = g.Key,
                 cantidad = g.Count(),
                 items = g
              };

In this case you return all the codes and you can validate using the group by if there are repeated

But if you need it you can also filter

var result = from item in dbcontext.NombreTabla
              group item by item.codigo into g
              where g.Count() > 1
              select new {
                 codigo = g.Key,
                 items = g
              };

to go through the list you would use

foreach(var codGroup in result){
    //aqui puede acceder al codGroup.codigo, para conocer por cual esta agrupando
    foreach(var item in codGroup.items){
        //aqui puedes acceder a cada registro que tiene ese codigo
    }
}

Group query results

    
answered by 26.11.2018 в 03:04