我试图在扩展方法中使用一个非常简单的预编译查询来使用 LINQ to SQL 检索数据。SytelineRepository 是我使用外部映射文件的自定义 DataContext,我正在查询 JobOrders 表。我已经使用自定义 DataContext 很长时间没有问题,并且执行相同的查询也没有问题;我只是想提高性能。但是,当我尝试使用 CompiledQuery.Compile 时,我得到下面的 ArgumentException。
这是代码:
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);
}
}
作为对比,这有效:
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;
}
这是完整的错误:
System.ArgumentException 未处理
Message=Property 'System.String ItemNumber' 没有为类型 '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()