Your code fragment may not fully describe the code you are using.
Instead, consider this snippet:
var dc = new myDataContext(); Contract.Assume(dc.Cars!= null); var models = dc.Cars.WithOwner('Jim').Select(c => c.Model); public static IQueryable<Car> WithOwner(this IQueryable<Car> cars, string owner) { Contract.Requires(cars != null); return cars.Where(c => c.Owner == owner); }
In this case, the runtime will probably complain about the warning you mentioned, but it does not complain that Cars may be empty, it complains about the result from WithOwner (passed to Select ), possibly zero.
You can satisfy the runtime by ensuring that the result of your extension method is not zero:
Contract.Ensures(Contract.Result<IQueryable<Car>>() != null);
This contract must be approved because Where does not return null, but instead returns Enumerable.Empty<T>() when there are no matches.
Jim counts
source share