I'm trying to use a very simple pre-compiled query within an extension method to retrieve data using LINQ to SQL. SytelineRepository is my custom DataContext that uses external mapping files, and I'm querying the JobOrders table. I've been using the custom DataContext for a long time without issue and executing the same query with no problem; I'm just trying to improve performance. However when I try to use CompiledQuery.Compile I get the ArgumentException below.
Here's the code:
public static class Queries
{
public static readonly Func<SytelineRepository, String, IQueryable<JobOrder>> GetOpenJobOrdersForItemQuery =
CompiledQuery.Compile(
(SytelineRepository r, String itemNumber) =>
from job in r.JobOrders
where job.ItemNumber == itemNumber
select job
);
public static IQueryable<JobOrder> GetOpenJobOrdersForItem(this SytelineRepository r, System.String itemNumber)
{
return GetOpenJobOrdersForItemQuery(r, itemNumber);
}
}
As a contrast, this works:
public static IEnumerable<JobOrder> GetOpenJobOrdersForItem(this SytelineRepository r, System.String itemNumber)
{
return GetOpenJobOrdersForItemUncompiledQuery(r, itemNumber);
}
public static IQueryable<JobOrder> GetOpenJobOrdersForItemUncompiledQuery(SytelineRepository r, String itemNumber)
{
return
from job in r.JobOrders
where job.ItemNumber == itemNumber
select job;
}
Here's the full error:
System.ArgumentException was unhandled
Message=Property 'System.String ItemNumber' is not defined for type 'System.Linq.IQueryable`1[Mpicorp.SytelineDataModel.JobOrder]'
at System.Linq.Expressions.Expression.Property(Expression expression, PropertyInfo property)
at System.Data.Linq.SqlClient.SqlBinder.Visitor.AccessMember(SqlMember m, SqlExpression expo)
at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitExpression(SqlExpression expr)
at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitBinaryOperator(SqlBinary bo)
at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitExpression(SqlExpression expr)
at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitSelect(SqlSelect select)
at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitIncludeScope(SqlIncludeScope scope)
at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
at System.Data.Linq.SqlClient.SqlBinder.Bind(SqlNode node)
at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(ResultShape resultShape, Type resultType, SqlNode node, ReadOnlyCollection`1 parentParameters, SqlNodeAnnotations annotations)
at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations)
at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Compile(Expression query)
at System.Data.Linq.CompiledQuery.ExecuteQuery(DataContext context, Object[] args)
at System.Data.Linq.CompiledQuery.Invoke[TArg0,TArg1,TResult](TArg0 arg0, TArg1 arg1)
at Mpicorp.SytelineDataModel.Queries.GetOpenJobOrdersForItem(SytelineRepository r, String itemNumber) in C:\SVN\Mpicorp.SytelineDataModel\trunk\SytelineDataModel\Queries.cs:line 21
at Mpicorp.SytelineDataModel.SytelineRepository.GetTimePhasedInventory(String itemNumber, Boolean includeForecast) in C:\SVN\Mpicorp.SytelineDataModel\trunk\SytelineDataModel\SytelineRepository.cs:line 242
at RepriceWorkbench.TimePhasedInventoryForm..ctor(SytelineRepository repository, String itemNumber) in C:\SVN\spirepriceutility\trunk\src\POWorkbench\TimePhasedInventoryForm.cs:line 32
at RepriceWorkbench.TimePhasedMenuForm.goBtn_Click(Object sender, EventArgs e) in C:\SVN\spirepriceutility\trunk\src\POWorkbench\TimePhasedMenuForm.cs:line 25
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at RepriceWorkbench.Program.Main() in C:\SVN\spirepriceutility\trunk\src\POWorkbench\Program.cs:line 66
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
The comment from usr led me to review the definition underlying the code I posted, and that's where I stumbled across the long-forgotten fact that my table stores several different objects and I was differentiating between them using the repository's accessors, like this:
public IQueryable<JobOrder> JobOrders
{
get { return GetTable<JobOrder>().Where(j => j.JobType == 'J'); }
}
Changing the method to use GetTable<>(), as in the following fixed the issue.
public static readonly Func<SytelineRepository, String, IEnumerable<JobOrder>> GetOpenJobOrdersForItemQuery =
CompiledQuery.Compile(
(SytelineRepository r, String itemNumber) =>
from job in r.GetTable<JobOrder>()
where job.ItemNumber == itemNumber && job.JobType == 'J' && (job.Status == "F" || job.Status == "R")
select job
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With