How to avoid "source !=null" when using Code Contracts and Linq To Sql? -
I have the following code using a normal data reference that works great:
var dc = new myDataContext (); Contract.assum (DC. Car! = Null); Var Cars = (Select from DC Carr where C. Honor == 'Jim' c) .Oolist ();
However, when I convert the filter to an extension method like this:
var dc = new myDataContext (); Contract.assum (DC. Car! = Null); Var car = DC Carous Owner ('gym'); Public stable IQueryable & lt; Cars & gt; Onner (this IQueryable & lt; car & gt; car; string owner) {contract. Requirements (cars! = Null); Return cars. Where (c => c. Owner == owner); }
I get the following warning:
Warning: CodeContract: Unproduct Required: Source! = Void
My guess is that your warning is caused by the owner parameter rather than cars To find out whether the owner is not empty, add a pre-condition to theOwner method.
Public stable IQueryable & lt; Cars & gt; Together Owner (IQueryable & lt; car & gt; car, string owner) {contract. Requirements (cars! = Null); Contract.Requires (string.isNullOrEmpty (Owner)!); Return cars. Where (c => c. Owner = owner); }
In your first code sample, you have 'Jim' hard-coded, so there is no problem because there is nothing that can be zero.
In your second example, you made a method for which the static compiler can not prove that the source (owner) will never 'empty' because other codes can say it with invalid values.
Comments
Post a Comment