How to avoid "source !=null" when using Code Contracts and Linq To Sql?

Posted by Florian on Stack Overflow See other posts from Stack Overflow or by Florian
Published on 2010-06-16T02:05:20Z Indexed on 2010/06/16 2:12 UTC
Read the original article Hit count: 488

Filed under:
|

I have the following code using a normal data context which works great:

var dc = new myDataContext();
Contract.Assume(dc.Cars!= null);
var cars = (from c in dc.Cars
            where c.Owner = 'Jim'
            select c).ToList();

However when I convert the filter to an extension method like this:

var dc = new myDataContext();
Contract.Assume(dc.Cars!= null);
var cars = dc.Cars.WithOwner('Jim');

public static IQueryable<Car> WithOwner(IQueryable<Car> cars, string owner)
{
    Contract.Requires(cars != null);
    return cars.Where(c => c.Owner = owner);
}

I get the following warning:

warning : CodeContracts: requires unproven: source != null

© Stack Overflow or respective owner

Related posts about linq-to-sql

Related posts about code-contracts