我正在试图使用一个非常简单的预编译查询来使用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=类型'System.Linq.IQueryable`1[Mpicorp.SytelineDataModel.JobOrder]'中没有定义属性'System.String ItemNumber'
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()