1

Hi I have a table where there is a RespondBy property which is of DateTime and is Nullable. Here is the linq I'm trying to run over EF6:

IEnumerable<Enquiry> ASAPEnquiries = db.Enquiries
       .Where(enq => enq.RespondBy == null && enq.JobCostings.Count == 0)
       .OrderBy(enq => enq.FlReference);

However, when I run if (ASAPEnquiries.Count() > 0) I get an error stating Nullable object must have a value. How would one query the database using linq if you want to check null DateTime columns?

Thank you.

EDIT:

The SQL that is produced by EF when tested in MSSMS brings back the desired result FYI. SQL Produced:

SELECT 
    [Project1].[Id] AS [Id], 
    [Project1].[FlReference] AS [FlReference], 
    [Project1].[EnquiryDate] AS [EnquiryDate], 
    [Project1].[ContactName] AS [ContactName], 
    [Project1].[ProjectReference] AS [ProjectReference], 
    [Project1].[EnquiryDetails] AS [EnquiryDetails], 
    [Project1].[RespondBy] AS [RespondBy], 
    [Project1].[CreatedBy] AS [CreatedBy], 
    [Project1].[Created] AS [Created], 
    [Project1].[ModifiedBy] AS [ModifiedBy], 
    [Project1].[Modified] AS [Modified], 
    [Project1].[RowVersion] AS [RowVersion], 
    [Project1].[Enquiry_Customer] AS [Enquiry_Customer], 
    [Project1].[Enquiry_EnquiryStatus] AS [Enquiry_EnquiryStatus]
    FROM ( SELECT 
        [Extent1].[Id] AS [Id], 
        [Extent1].[FlReference] AS [FlReference], 
        [Extent1].[EnquiryDate] AS [EnquiryDate], 
        [Extent1].[ContactName] AS [ContactName], 
        [Extent1].[ProjectReference] AS [ProjectReference], 
        [Extent1].[EnquiryDetails] AS [EnquiryDetails], 
        [Extent1].[RespondBy] AS [RespondBy], 
        [Extent1].[CreatedBy] AS [CreatedBy], 
        [Extent1].[Created] AS [Created], 
        [Extent1].[ModifiedBy] AS [ModifiedBy], 
        [Extent1].[Modified] AS [Modified], 
        [Extent1].[RowVersion] AS [RowVersion], 
        [Extent1].[Enquiry_Customer] AS [Enquiry_Customer], 
        [Extent1].[Enquiry_EnquiryStatus] AS [Enquiry_EnquiryStatus], 
        (SELECT 
            COUNT(1) AS [A1]
            FROM [dbo].[JobCostings] AS [Extent2]
            WHERE [Extent1].[Id] = [Extent2].[JobCosting_Enquiry]) AS [C1]
        FROM [dbo].[Enquiries] AS [Extent1]
    )  AS [Project1]
    WHERE ([Project1].[RespondBy] IS NULL) AND (0 = [Project1].[C1])
    ORDER BY [Project1].[FluidReference] ASC

Also, if I iterate over the collection, I get no error so it seems to be something to do with calling .Count() on the collection.

11
  • Thank you. I sort of understand why the error is occuring, I just do not know the best practise to achieve what I'm trying to do. Apr 16, 2015 at 20:40
  • Try this:var ASAPEnquiries = db.Enquiries .Where(enq => !enq.RespondBy.HasValue) .OrderBy(enq => enq.FlReference).ToList(); just to track down the error. Tell us if you get any errors when you step over that line. Apr 16, 2015 at 21:13
  • can you try enq => enq.RespondBy != null to see what haeppens
    – renakre
    Apr 16, 2015 at 21:15
  • @Robert, Yeah, the same I'm afraid. Apr 16, 2015 at 21:15

3 Answers 3

1

Use the HasValue property to check if a Nullable has value.

IEnumerable<Enquiry> ASAPEnquiries = db.Enquiries
       .Where(enq => enq.RespondBy.HasValue && enq.JobCostings.Count == 0)
       .OrderBy(enq => enq.FlReference);
3
  • I tried IEnumerable<Enquiry> ASAPEnquiries = db.Enquiries.Where(enq => enq.RespondBy.HasValue == false && enq.JobCostings.Count == 0).OrderBy(enq => enq.FluidReference); already and I get the same result as it happens. Apr 16, 2015 at 20:37
  • @paulpitchford, ` == false` is unnecessary!
    – Maciej Los
    Apr 16, 2015 at 20:48
  • Is it? Effectively I require records where RespondBy do not have a value. would I use !enq.RespondBy.HasValue? <-- This does the same as the above too. Apr 16, 2015 at 20:53
0

Please make sure that your Enquiry class assign value to the JobCostings property in it's constructor e.g.

public Enquiry()
{
    this.JobCostings = new HashSet<JobCosting>();
    //.......
}

and try this one :

IEnumerable<Enquiry> ASAPEnquiries = db.Enquiries
       .Where(enq => enq.RespondBy == null && !enq.JobCostings.Any())
       .OrderBy(enq => enq.FlReference);
0

It would seem I have egg on my face. The if statement was calling a void where I was then using item.RespondBy.Value.ToShortDateTime(). How embarresing. Thank you all for your help though, I appreciate your time.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.