如何解决动态LINQ ParseException:“标识符预期”

时间:2013-05-22 21:09:32

标签: parsing lambda export-to-excel dynamic-linq mvcjqgrid

我收到通用错误消息System.Linq.Dynamic.ParseException:期望的标识符。

我应该检查什么才能解决它?错误不是很详细。

我确实检查了ParseLambda方法中的源和选择器参数,它们携带的T-SQL似乎有效,并且在SQL Management Studio中执行正常。

 Server Error in '/' Application.
________________________________________
Identifier expected 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Linq.Dynamic.ParseException: Identifier expected

Source Error: 
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace: 

[ParseException: Identifier expected]
   System.Linq.Dynamic.ExpressionParser.ValidateToken(TokenId t, String errorMessage) in C:\Projects\EDgE\EDgE-EDGE_Parsippany\WebUI\DynamicQuery\Dynamic.cs:1952
   System.Linq.Dynamic.ExpressionParser.GetIdentifier() in C:\Projects\EDgE\EDgE-EDGE_Parsippany\WebUI\DynamicQuery\Dynamic.cs:1941
   System.Linq.Dynamic.ExpressionParser.ParseNew() in C:\Projects\EDgE\EDgE-EDGE_Parsippany\WebUI\DynamicQuery\Dynamic.cs:1038
   System.Linq.Dynamic.ExpressionParser.ParseIdentifier() in C:\Projects\EDgE\EDgE-EDGE_Parsippany\WebUI\DynamicQuery\Dynamic.cs:966
   System.Linq.Dynamic.ExpressionParser.ParsePrimaryStart() in C:\Projects\EDgE\EDgE-EDGE_Parsippany\WebUI\DynamicQuery\Dynamic.cs:867
   System.Linq.Dynamic.ExpressionParser.ParsePrimary() in C:\Projects\EDgE\EDgE-EDGE_Parsippany\WebUI\DynamicQuery\Dynamic.cs:848
   System.Linq.Dynamic.ExpressionParser.ParseUnary() in C:\Projects\EDgE\EDgE-EDGE_Parsippany\WebUI\DynamicQuery\Dynamic.cs:844
   System.Linq.Dynamic.ExpressionParser.ParseMultiplicative() in C:\Projects\EDgE\EDgE-EDGE_Parsippany\WebUI\DynamicQuery\Dynamic.cs:798
   System.Linq.Dynamic.ExpressionParser.ParseAdditive() in C:\Projects\EDgE\EDgE-EDGE_Parsippany\WebUI\DynamicQuery\Dynamic.cs:771
   System.Linq.Dynamic.ExpressionParser.ParseComparison() in C:\Projects\EDgE\EDgE-EDGE_Parsippany\WebUI\DynamicQuery\Dynamic.cs:702
   System.Linq.Dynamic.ExpressionParser.ParseLogicalAnd() in C:\Projects\EDgE\EDgE-EDGE_Parsippany\WebUI\DynamicQuery\Dynamic.cs:689
   System.Linq.Dynamic.ExpressionParser.ParseLogicalOr() in C:\Projects\EDgE\EDgE-EDGE_Parsippany\WebUI\DynamicQuery\Dynamic.cs:676
   System.Linq.Dynamic.ExpressionParser.ParseExpression() in C:\Projects\EDgE\EDgE-EDGE_Parsippany\WebUI\DynamicQuery\Dynamic.cs:662
   System.Linq.Dynamic.ExpressionParser.Parse(Type resultType) in C:\Projects\EDgE\EDgE-EDGE_Parsippany\WebUI\DynamicQuery\Dynamic.cs:629
   System.Linq.Dynamic.DynamicExpression.ParseLambda(ParameterExpression[] parameters, Type resultType, String expression, Object[] values) in C:\Projects\EDgE\EDgE-EDGE_Parsippany\WebUI\DynamicQuery\Dynamic.cs:166
   System.Linq.Dynamic.DynamicExpression.ParseLambda(Type itType, Type resultType, String expression, Object[] values) in C:\Projects\EDgE\EDgE-EDGE_Parsippany\WebUI\DynamicQuery\Dynamic.cs:161
   System.Linq.Dynamic.DynamicQueryable.Select(IQueryable source, String selector, Object[] values) in C:\Projects\EDgE\EDgE-EDGE_Parsippany\WebUI\DynamicQuery\Dynamic.cs:34
   WebUI.Controllers.VoterController.ExportToExcel() in C:\Projects\EDgE\EDgE-EDGE_Parsippany\WebUI\Controllers\VoterController.cs:2288
   lambda_method(Closure , ControllerBase , Object[] ) +62
   System.Web.Mvc.<>c__DisplayClass1.<WrapVoidAction>b__0(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
   Castle.Proxies.ControllerActionInvokerProxy.InvokeActionMethod_callback(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +42
   Castle.Proxies.Invocations.ControllerActionInvoker_InvokeActionMethod.InvokeMethodOnTarget() +208
   Castle.DynamicProxy.AbstractInvocation.Proceed() +56
   Glimpse.Mvc3.Interceptor.InvokeActionMethodInterceptor.Intercept(IInvocation invocation) +408
   Castle.DynamicProxy.AbstractInvocation.Proceed() +94
   Castle.Proxies.ControllerActionInvokerProxy.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +222
   System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
   System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
   System.Web.Mvc.Controller.ExecuteCore() +116
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8970141
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

________________________________________
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272

第1952行的ParseException:

ParseException at line 1952

调用堆栈异常源:

enter image description here

1 个答案:

答案 0 :(得分:1)

好的,我不知道这是System.Linq.Dynamic库中的Microsoft错误还是滥用SQL列别名,但在这种特殊情况下,解析器失败:

 Select Col1 as First_Name, Col2 as Last_Name, col3 as 2012_P_Customer
 From MyTable

将Col3解析为2012_P_Customer时,System.Linq.Dynamic解析器失败。它不喜欢2012号码作为前缀。

当我将其更改为“col3 as P_Customer_2012”时,它工作正常。

我知道为SQL列添加前缀是一种不好的做法,但我不知道对列别名有效。