Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 5.0. What is wrong with my Query?

This is my code:

public DateTime GibSomeStartDate(IEnumerable<int> partnerNumbers, DateTime startTime)
{
     var contractsStartDate = from contract in this.databaseContext.Contract
                              where partnerNumbers.Contains(contract.Pnr) 
                                 && contract.SomeDateTime >= startTime
                              select contract.SomeDateTime;
}

if I call contractsStartDate.Min() an Exception occurs:

Unable to create a null constant value of type 'System.Collections.Generic.IEnumerable`1'. Only entity types, enumeration types or primitive types are supported in this context.

What is wrong with my Query?

  • the contractsStartDate is of type System.Data.Entity.Infrastructure.DbQuery

  • EF 5.0

  • databaseContext is child of System.Data.Entity.DbContext

like image 394
MikroDel Avatar asked Oct 22 '22 15:10

MikroDel


1 Answers

I know this error. Just Make sure partnerNumbers is not null. You're passing a null value for this parameter, but Linq-to-entities can't translate that value to anything meaningful.

if (partnerNumbers == null)
{
    throw new ArgumentNullException("partnerNumbers");
}

An additional bonus advice:

If SomeDateTime is not nullable and there are no entries in your enumeration, then you'll get an exception on calling Min(). Casting SomeDateTime to the nullable type in your query will work, then you get null when there are no entries.

like image 180
JustAnotherUserYouMayKnow Avatar answered Oct 24 '22 09:10

JustAnotherUserYouMayKnow