Home > Back-end >  Linq query showing more data that the sql alternative
Linq query showing more data that the sql alternative

Time:09-29

I have an linq query like this :

var query = from Romm in RoMM
                        
                        join rfrsa in RoMmfrsa  on Romm.RoMmid equals rfrsa.RoMmid
                        join frsa in Frsa on rfrsa.Frsaid equals frsa.Fraid
                        join fra in Fra on frsa.Fraid equals fra.Fraid
                        where Romm.ActTypeId == 2 && Romm.SegmentId == 4
                        select new
                        {
                            Romm.ActTypeId,
                            Romm.RoMmid,
                            frsa.Fraid,
                            frsa.Frsaid,
                            Romm.ImpactId
                        };

And I have sql code as below :

SELECT romm.ROMMID 

     , frsa.FRAID 

     , frsa.FRSAID 

     , romm.ImpactID 

  FROM RoMM AS romm 

 INNER 

  JOIN RoMMFRSA AS rfrsa 

    ON romm.RoMMID = rfrsa.RoMMID 

 INNER 

  JOIN FRSA AS frsa 

    ON rfrsa.frsaid = frsa.frsaid 

 INNER 

  JOIN FRA AS fra 

    ON frsa.FRAID = fra.FRAID 

 WHERE romm.acttypeid = 2 

   AND romm.segmentid = 4

The SQL only shows one row (which is correct) , the linq shows the correct row and then it displays about another 3 rows which is not what we need. I need the linq to show one row which is correct with the sql... Is this because of maybe many-many relationships ?

Any help would be awesome.

CodePudding user response:

Looks like a typo in either the C# or the SQL join:

SQL: ON rfrsa.frsaid    =   frsa.frsaid
C#:     rfrsa.Frsaid equals frsa.Fraid
                                 ^^^^^^ 
                              mismatch here
  • Related