Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq Sql Int null equal to null. Not returning rows

Tags:

c#

sql

linq

I have a linq query with a where clause as below

var ExistingGroupDataSource = (from ppatg in dbContext.XXXXXXXXX
                               join pd1 in dbContext.XXXXXXXXXXX on ppatg.ScheduleID equals pd1.ScheduleID 
                               where  pd1.GroupID == null
                               select
                               new 
                               {
                                FinishPDI = pd1.ProductionDateID,
                                FinishingDate = pd1.WorkFlowDate,
                                ppatg.GroupName,
                                ppatg.PNTGroupID
                               });

In the database GroupID is an int that can be null. The linq query returns rows without the filtered where clause but none when I include the where clause. There are null values in the GroupId column in the database.

It is definitely this statement that produces no results. All the literature on the subject online says that this is equivalent to

pd1.GroupID is null // in sql

I am getting results that contradict this

Sql code is

 select pd1.ProductionDateID as FinishPDI, pd1.WorkflowDate as FinishingDate,GroupName ,PNTGroupId 
 from XXXXXXXXXXXX 
 inner join XXXXXXXXXXXX pd1 on 
 XXXXXXXXXXXX.ScheduleId = pd1.ScheduleID
 where pd1.GroupID is null 
like image 426
Darren Avatar asked Jan 18 '26 19:01

Darren


1 Answers

You can combine your where with the join, which should give you the expected results:

var ExistingGroupDataSource = (from ppatg in dbContext.XXXXXXXXX
                               join pd1 in dbContext.XXXXXXXXXXX.Where(p => !p.GroupId.HasValue) on ppatg.ScheduleID equals pd1.ScheduleID 
                               select
                               new 
                               {
                                FinishPDI = pd1.ProductionDateID,
                                FinishingDate = pd1.WorkFlowDate,
                                ppatg.GroupName,
                                ppatg.PNTGroupID
                               });
like image 103
ZippyZippedUp Avatar answered Jan 21 '26 08:01

ZippyZippedUp