Making my first attempt at precompiling a linq query and it looks like I'm doing something wrong.
I want to use the precompiled query in a validator query that will wind up being called around 14k times per use. I want to instantiate the query in the constructor (again, first time using these so not sure if this is the proper use) so the class object will be able to call the compiled query instead.
Here is the Func
as a property and instantiated in the constructor
private static Func<DataContext, ZipCodeTerritory, IQueryable<ZipCodeTerritory>> SearchEffectiveDate;
public ZipCodeValidatorCompiled()
{
SearchEffectiveDate = CompiledQuery.Compile((DataContext db, ZipCodeTerritory zipCode)
=>
from z in db.GetTable<ZipCodeTerritory>()
where z.DrmTerrDesc.Equals(zipCode.DrmTerrDesc) &&
z.IndDistrnId.Equals(zipCode.IndDistrnId) &&
z.StateCode.Equals(zipCode.StateCode) &&
(z.ZipCode.Equals(null) ? z.ZipCode.Equals(null) : z.ZipCode.Equals(zipCode.ZipCode))
select z
);
}
And here is how it is called in one of the validation methods
private static string ValidateEffectiveDate(ZipCodeTerritory zipCode)
{
using (var _db = new AgentResources())
{
IQueryable<ZipCodeTerritory> terrList = SearchEffectiveDate(_db, zipCode);
foreach (var zip in terrList)
{
if (zip.EffectiveDate >= zipCode.EndDate)
{
return
"End Date must be greater than Effective Date of any other record sharing the same DRM Territory Description, Territory, State and Zip Code; ";
}
}
}
return null;
}
The issue I'm running into is with this statement in the validation method
IQueryable<ZipCodeTerritory> terrList = SearchEffectiveDate(_db, zipCode);
Visual Studio SearchEffectiveDate(_db, zipCode)
is giving me the following errors:
- Error 20 Delegate 'System.Func>' has some invalid arguments
- Error 21 Argument 1: cannot convert from 'Monet.Models.AgentResources' to 'System.Data.Linq.DataContext'
I am using this tutorial on speeding up Linq queries.