3

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 
6
  • Is GroupID in your Model a nullable type? Commented Dec 5, 2017 at 3:40
  • Yes, in the model GroupID is a int? Commented Dec 5, 2017 at 3:42
  • It should be int? GroupID. Commented Dec 5, 2017 at 3:43
  • Can you show sql code? Commented Dec 5, 2017 at 3:47
  • @Backs added sql code Commented Dec 5, 2017 at 3:56

1 Answer 1

1

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
                               });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.