remove duplicate rows in linq from viewmodel mvc 4

How i can remove my duplicate rows from my ViewModel

view model:

public class ClientesPorEvento 
{
    public eventocliente eventocliente { get; set; }
    public cliente cliente { get; set; }
    public string NombreSubrecurso { get; set; }
    public string NombreRecurso { get; set; }
    public IEnumerable<precioventa> preciosventa { get; set; }
}

The Controller Left Join Query Code:

public ActionResult ClientesPorEvento(int idEvento)
{
//se hace un left join para que se autollenen las filas null donde las columnas tienen datos
    IEnumerable<ClientesPorEvento> clientesPorEvento =
       (from c in db.cliente
        join ec in db.eventocliente on c.idCliente equals ec.idCliente
        join pv in db.precioventa on ec.idVenta equals pv.idVenta into pventa
            from pv in pventa.DefaultIfEmpty()
        join rc in db.recursocliente on ec.idVenta equals rc.idVenta into recuc
            from rc in recuc.DefaultIfEmpty()
            join sr in db.subrecurso on rc.idSubrecurso equals sr.idSubrecurso into subre
                from sr in subre.DefaultIfEmpty()
                join r in db.recurso on sr.idRecurso equals r.idRecurso into recur
                    from r in recur.DefaultIfEmpty()
        where ec.idEvento == idEvento
        select new ClientesPorEvento {
            preciosventa = pventa,
            eventocliente = ec,
            cliente = c,
            NombreSubrecurso = sr.nombre == null ? "" : sr.nombre,
            NombreRecurso = r.nombreRecurso == null ? "(No Reservas)" : r.nombreRecurso
        }).OrderBy(m => m.eventocliente.nroReserva);

    return PartialView(clientesPorEvento);
}

The Result is like this: enter image description here

I dont know why it gives me duplicates rows, and it happend only if the column has 2 values ("preciosventa" From ViewModel), and last I tried to remove the duplicates rows with groupby and hashSet, without success. Thanks a lot!!!

Upvotes: 1

Views: 800

Answers (1)

Marco
Marco

Reputation: 23937

I'm pretty sure the grouping in your comment is correct and will work. However you forgot to actually select something from that grouping, so that you still had all your duplicates - just grouped together:

var distinctClientsPerEvent = clientesPorEvento.GroupBy(m => m.cliente.nombreCliente)
                                               .Select(x => x.FirstOrDefault());

Your grouping consists of 1 or more objects of type ClientsPorEvento (ClientsPerEvent? My spanish isn't the best). IF the grouping has more then one object in it, that means all others are duplicates. We just select the first one and will have a duplicate free ViewModel.

One more thing: As stated in my comment, I wouldn't group on something that is subject to change, like a name. Ids are permanent, use those. If in your example "Maria" orders some tickets, marries someone, changes her name in your system and orders some more tickets, your grouping will fail, because her name changed. This is highly unlikely, but better be safe than sorry.

Upvotes: 2

Related Questions