c#/Linq: Remove duplicates in a list, using group and orderby in linq

1 comment

I had some really ugly code to remove duplicates in a list.

Here's my take at doing the same with linq and grouping.

Probably could be improved, but I like this way better then the ugly original.

I find it really easy to read too.

Because of the way the original was constructed, I used a couple of tries to get to this.

My first attempt used the same way of thinking as the original,

and that wasn't much prettier then the old c# 2.0 code.

private static List<NytegningAktoerRetur> GetSisteTilbudPåSammeNrLinq(List<NytegningAktoerRetur> liste)

{

    var MidlertidigNrTilbud = from t in liste

                         group t by t.MidlertidigForsNr;

    var FinListe = new List<NytegningAktoerRetur>();

    foreach (var midlert in MidlertidigNrTilbud)

    {

        if (midlert.Count() > 1)

        {

            //Finn siste

            var t = from tilbud in midlert

                    orderby FinnOppgaveID(tilbud.TilbudID) descending, tilbud.DatoRegistrert descending                   

                    select tilbud;

            FinListe.Add(t.First());

        }

        else

            FinListe.Add(midlert.First());

    }

    return FinListe;

}

The original is here for warning purposes only :)

Don't try this at home…:

private static void GetSisteTilbudPåSammeNr(FinnTilbudAktoerReturMelding retur, bool InkluderStartedeTilbud)

{

    Dictionary<string, FinnTilbudHolder> liste = new Dictionary<string, FinnTilbudHolder>();

    List<string> sakstartet = new List<string>();

    FinnTilbudHolder h;

    foreach (NytegningAktoerRetur r in retur.TilbudsListe)

    {

        int OppgaveID = FinnOppgaveID(r.TilbudID);

        h = new FinnTilbudHolder();

        h.Tilbud = r;

        h.OppgaveID = OppgaveID;

        //finn nyeste

        if (liste.ContainsKey(h.Tilbud.MidlertidigForsNr))

        {

            //finn siste basert på oppgaveid

            FinnTilbudHolder org = liste[h.Tilbud.MidlertidigForsNr];

            if (OppgaveID > org.OppgaveID)

            { //denne (r) er nyest, skal erstatte org i listen

                liste[h.Tilbud.MidlertidigForsNr] = h;

            }

            else if (org.OppgaveID > OppgaveID) //org er nyest basert på oppgaveid

            {

                //beholder org

            }

            else if (org.OppgaveID == 0 && OppgaveID == 0) //ingen oppgaveid angitt, returner nyeste på dato

            {

                //hvis nyerere "vinner" den (r), ellers ingen endring

                if (h.Tilbud.DatoRegistrert > org.Tilbud.DatoRegistrert)

                    liste[h.Tilbud.MidlertidigForsNr] = h;

            }

        }

        else

        {

            liste.Add(h.Tilbud.MidlertidigForsNr, h);

        }

        //husk de som har startet sak

        if (!String.IsNullOrEmpty(h.Tilbud.Saksnr)) sakstartet.Add(h.Tilbud.MidlertidigForsNr);

    }

    if (!InkluderStartedeTilbud)

    {

        //fjern de som har startet sak

        foreach (string forsnr in sakstartet)

        {

            liste.Remove(forsnr);

        }

    }

    List<NytegningAktoerRetur> l = new List<NytegningAktoerRetur>();

    foreach (FinnTilbudHolder f in liste.Values)

    {

        l.Add(f.Tilbud);

    }

    retur.TilbudsListe.Clear();

    retur.TilbudsListe.AddRange(l.ToArray());

}

That's it for the old retarted ehh I mean retired code :)

To be fair, I also moved the code of InkluderStartedeTilbud out of this function, it goes like this:

if (!param.InkluderStartedeTilbud)

{

    //Fjern startede tilbud

    retur.TilbudsListe.RemoveAll(g => !String.IsNullOrEmpty(g.Saksnr));

}

I just moved it out to make the function more clear.

The refactoring to linq also allowed me to remove the FinnTilbudHolder class, as it was just used to keep record of each records id,

because it was an expensive operation to get it (ws call).

In addition and it was now easy to just get the id when I knew that there was more then one record with the same number.

I could (should)  have done that with the old code too, but now it was real easy to do because of the grouping and count.

I'm happy with the new and refactored linq version, and the easy grouping, counting and sorting in linq.

Rgds

HM

Posted via email from Henris blogg

1 comment :

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru Dot Net Training in Chennai. Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete