Avoid duplicated list data

Asked

Viewed 1,667 times

1

I need a list of musicians without repetitions, however it is bringing duplicate values, according to the amount of songs that each Musician has, how do I to be brought only one Musician?

public ActionResult SelecaoMusico()
{
    List<Musico> musicos = new List<Musico>();
    Musico musico;
    List<Musica> musicas = db.Musicas.ToList();

    foreach(var musica in musicas){
        int idMusica = musica.MusicaID;
        int musicoIdMusica = musica.MusicoID;

        musico = db.Musico.Where(p => p.MusicoID.Equals(musicoIdMusica)).SingleOrDefault();
        musicos.Add(musico);
    }

    return Json(musicos);
}
  • You’re repeating this within your foreach. You’re adding a musician to the list to every song that has the musician id included. there’s your problem.

  • is yes, but I’ve corrected that, maybe not in the best way, but the answer is low. Thanks @Edvaldofarias

3 answers

2


public ActionResult SelecaoMusico()
{
    List<Musico> musicos = new List<Musico>();
    Musico musico;
    List<Musica> musicas = db.Musicas.ToList();

    var listaMusicasSemDuplicada = musicas.GroupBy(x => x.MusicoID).Select(y => y.First());
    foreach(var musica in listaMusicasSemDuplicada){
        int idMusica = musica.MusicaID;
        int musicoIdMusica = musica.MusicoID;

        musico = db.Musico.Where(p => p.MusicoID.Equals(musicoIdMusica)).SingleOrDefault();
        musicos.Add(musico);
    }

    return Json(musicos);
}

That way there will be no repetition.

1

I did one inside another for:

for (int i = 0; i < musicos.Count; i++)
{
    for (int j = i + 1; j < musicos.Count; j++)
    {
        if (musicos[i].PessoaID == musicos[j].PessoaID)
            musicos.Remove(musico);
    }//fim do for j
}//fim do for i

leaving the code like this:

public ActionResult SelecaoMusico()
{
List<Musico> musicos = new List<Musico>();
Musico musico;
List<Musica> musicas = db.Musicas.ToList();

foreach(var musica in musicas){
    int idMusica = musica.MusicaID;
    int musicoIdMusica = musica.MusicoID;

    musico = db.Musico.Where(p => p.MusicoID.Equals(musicoIdMusica)).SingleOrDefault();
    musicos.Add(musico);

for (int i = 0; i < musicos.Count; i++)
{
    for (int j = i + 1; j < musicos.Count; j++)
    {
         if (musicos[i].MusicoID == musicos[j].MusicoID)
             musicos.Remove(musico);
         }//fim do for j
    }//fim do for i
}

return Json(musicos);
}
  • This looks more like a doozy. first you add and then remove, within 3 loops still.. see the answer below if it helps, if it does not signal to see something better

  • worked out! too long for me fucked up

0

Instead of using logic, you could use classes that implements ISet<T>, the HashSet<T>, For example, it does not accept duplicates by default being a good option. However it does not work with indexes, if you want to work with them it is good to give one. Tolist().

Reference: https://msdn.microsoft.com/pt-br/library/bb359438(v=vs.110). aspx

Browser other questions tagged

You are not signed in. Login or sign up in order to post.