Entity Framework 5.0. What is wrong with my request?

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 there contractsStartDate.Min()is an Exception:

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 request?

  • contractsStartDate has type System.Data.Entity.Infrastructure.DbQuery

  • EF 5.0

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

+5
source share
1 answer

I know this mistake. Just make sure it is partnerNumbersnot zero. You pass a null value for this parameter, but Linq-to-entities cannot translate that value into anything meaningful.

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

Extra bonus tip:

If SomeDateTime- not nullableand there are no entries in your enumeration, you will get an exception when called Min(). Casting SomeDateTimeto a type nullablein your request will work, then you will get null when there are no records.

+3

All Articles