Home > Mobile >  What is the equivalent of this SQL query in linq
What is the equivalent of this SQL query in linq

Time:04-27

Select Distinct DVDTitle, CopyNumber from Actors a
inner join CastMembers b
on a.ActorNumber = b.ActorNumber
inner join DVDTitles c
on b.DVDNumber = c.DVDNumber
inner join DVDCopys d
on c.DVDNumber = d.DVDNumber;

How to write this sql query in linq: So far I've done this which returns value twice:

var actorList = from a in _db.Actors
                join b in _db.CastMembers
                on a.ActorNumber equals b.ActorNumber
                join c in _db.DVDTitles
                on b.DVDNumber equals c.DVDNumber
                join d in _db.DVDCopys
                on c.DVDNumber equals d.DVDNumber
                orderby c.DvdTitle
                                   

                select new Actor
                {
                     ActorNumber = a.ActorNumber,
                     ActorSurName = a.ActorSurName,
                     ActorFirstName = a.ActorFirstName,
                     DVDTitle = c.DvdTitle,
                     CopyNumber = d.CopyNumber
                };

I've also tried:

var actorList_01 = actorList.Distinct(); 

but the result is same.

CodePudding user response:

try this

 var matchingList = actorList 
                .GroupBy(x => x.ActorNumber )
                .Select(g => g.First())
                .ToList();
  • Related