Rewrite from .. where .. select to more compact one This is the code:

public DateTime GibSomeStartDate(IEnumerable&lt;int&gt; partnerNumbers, DateTime startTime)
{
     return (from contract in this.databaseContext.Contract
             where partnerNumbers.Contains(contract.Pnr) &amp;&amp; contract.SomeDateTime &gt;= startTime
             select contract.SomeDateTime).Min();
}


databaseContext Instance is child of Entity Framework DbContext

My refactorings objectives:


make it compact
check if from where select finds nothing. In this
situation Min() will cause an Excetion
This can be accomplished with a slight modification to your method signature and an extension method:

Here, the signature now can return null if there are no records selected:

public DateTime? GibSomeStartDate(IEnumerable&lt;int&gt; partnerNumbers, DateTime startTime)
{
    return (from contract in this.databaseContext.Contract
         where partnerNumbers.Contains(contract.Pnr) &amp;&amp; contract.SomeDateTime &gt;= startTime
         select contract.SomeDateTime).SafeMin(someDateTime =&gt; (DateTime?)someDateTime);
}


This extension method will trap Min's exception and return null instead:

public static U? SafeMin&lt;T, U&gt;(this IEnumerable&lt;T&gt; source, Func&lt;T, U?&gt; selector) where U : struct
{
    try
    {
        return source.Min(selector);
    }
    catch
    {
        return null;
    }
}