-1

Hello I have a table Artists and in that table there's the value BandID and I want to obtain BandIDs from the Artist list who occur twice in the Artist table. What's the correct LINQ query for this?

public class Artiest
    {
        public int ArtiestID { get; set; }
        public string Naam { get; set; } 

        public int InstrumentID { get; set; }
        public Instrument Instrument { get; set; }

        public int PopgroepID { get; set; }
        public Popgroep Popgroep { get; set; }

    }

I have tried to convert this to LINQ but I'm not able to translate it.

 var q =
        from g in arr.GroupBy(x => x)
        where g.Count() == 1
        select g.First();

1 Answers1

0

You should specify your question. What exactly is a "duplicate value" to you? Determined by which value? Of these duplicate artists, which one's PopGroepId do you want? One possible solution is

context.Artiests
    .GroupBy(x=>x.Naam)
    .Where(x=>x.Count()==2)
    .Select(x=>x.FirstOrDefault().PopGroepId);

This will get the PopGroepId of the first Artiest of whose name are exactly two entries in the database table.

DevilSuichiro
  • 1,049
  • 8
  • 21