2012-07-09 62 views
0
var result = (from p in productInQuery 
    join o in orderInfoQuery on p.refNo equals o.refNo 
    join x in productQuery on p.productNo equals x.no 
    join t in productOutQuery on p.no equals t.productInNo into productIn 
    from t in productIn.DefaultIfEmpty() 
    orderby o.processDate descending 
    select new 
    { 
     qty = p.qty, 
     dateIn = o.processDate, 
     dateOut = (DateTime?)(from m in orderInfoQuery where m.refNo == t.refNo select m.processDate).FirstOrDefault(), 
     etaDate = (DateTime?)(from w in orderInfoQuery where w.refNo == t.refNo select w.eta).FirstOrDefault(), 
    }) 

    if (etaDate != DateTime.MinValue) 
    { 
     result = result.Where(x => x.etaDate == etaDate); // Filter result 
    } 

    return result.ToArray(); 

我想要像上面那樣過濾linq結果,但它不起作用。Linq,filterling結果

有人知道,如何使它工作?

[編輯]

錯誤信息出現,

Server Error in '/' Application. 
Specified method is not supported. 
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.NotSupportedException: Specified method is not supported. 

Source Error: 


Line 147:   result = result.Where(s => s.etaDate == null); 
Line 148: 
Line 149:   return result.ToArray(); 
Line 150:  } 
Line 151: } 


Source File: WebUI\Models\Reports.cs Line: 149 

Stack Trace: 


[NotSupportedException: Specified method is not supported.] 
    MySql.Data.Entity.SqlGenerator.Visit(DbApplyExpression expression) +54 
    System.Data.Common.CommandTrees.DbApplyExpression.Accept(DbExpressionVisitor`1 visitor) +23 
    MySql.Data.Entity.SqlGenerator.VisitInputExpression(DbExpression e, String name, TypeUsage type) +65 
    MySql.Data.Entity.SelectGenerator.VisitInputExpressionEnsureSelect(DbExpression e, String name, TypeUsage type) +53 
    MySql.Data.Entity.SelectGenerator.Visit(DbProjectExpression expression) +132 
    System.Data.Common.CommandTrees.DbProjectExpression.Accept(DbExpressionVisitor`1 visitor) +23 
    MySql.Data.Entity.SqlGenerator.VisitInputExpression(DbExpression e, String name, TypeUsage type) +65 
    MySql.Data.Entity.SelectGenerator.VisitInputExpressionEnsureSelect(DbExpression e, String name, TypeUsage type) +53 
    MySql.Data.Entity.SelectGenerator.Visit(DbFilterExpression expression) +131 
    System.Data.Common.CommandTrees.DbFilterExpression.Accept(DbExpressionVisitor`1 visitor) +23 
    MySql.Data.Entity.SqlGenerator.VisitInputExpression(DbExpression e, String name, TypeUsage type) +65 
    MySql.Data.Entity.SelectGenerator.VisitInputExpressionEnsureSelect(DbExpression e, String name, TypeUsage type) +53 
    MySql.Data.Entity.SelectGenerator.Visit(DbProjectExpression expression) +132 
    System.Data.Common.CommandTrees.DbProjectExpression.Accept(DbExpressionVisitor`1 visitor) +23 
    MySql.Data.Entity.SqlGenerator.VisitInputExpression(DbExpression e, String name, TypeUsage type) +65 
    MySql.Data.Entity.SelectGenerator.VisitInputExpressionEnsureSelect(DbExpression e, String name, TypeUsage type) +53 
    MySql.Data.Entity.SelectGenerator.Visit(DbProjectExpression expression) +132 
    System.Data.Common.CommandTrees.DbProjectExpression.Accept(DbExpressionVisitor`1 visitor) +23 
    MySql.Data.Entity.SqlGenerator.VisitInputExpression(DbExpression e, String name, TypeUsage type) +65 
    MySql.Data.Entity.SelectGenerator.VisitInputExpressionEnsureSelect(DbExpression e, String name, TypeUsage type) +53 
    MySql.Data.Entity.SelectGenerator.Visit(DbProjectExpression expression) +132 
    System.Data.Common.CommandTrees.DbProjectExpression.Accept(DbExpressionVisitor`1 visitor) +23 
    MySql.Data.Entity.SqlGenerator.VisitInputExpression(DbExpression e, String name, TypeUsage type) +65 
    MySql.Data.Entity.SelectGenerator.VisitInputExpressionEnsureSelect(DbExpression e, String name, TypeUsage type) +53 
    MySql.Data.Entity.SelectGenerator.Visit(DbSortExpression expression) +168 
    System.Data.Common.CommandTrees.DbSortExpression.Accept(DbExpressionVisitor`1 visitor) +23 
    MySql.Data.Entity.SqlGenerator.VisitInputExpression(DbExpression e, String name, TypeUsage type) +65 
    MySql.Data.Entity.SelectGenerator.VisitInputExpressionEnsureSelect(DbExpression e, String name, TypeUsage type) +53 
    MySql.Data.Entity.SelectGenerator.Visit(DbProjectExpression expression) +132 
    System.Data.Common.CommandTrees.DbProjectExpression.Accept(DbExpressionVisitor`1 visitor) +23 
    MySql.Data.Entity.SelectGenerator.GenerateSQL(DbCommandTree tree) +169 
    MySql.Data.MySqlClient.MySqlProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree) +559 
    System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree) +147 
    System.Data.EntityClient.EntityCommandDefinition..ctor(DbProviderFactory storeProviderFactory, DbCommandTree commandTree) +521 

[EntityCommandCompilationException: An error occurred while preparing the command definition. See the inner exception for details.] 
    System.Data.EntityClient.EntityCommandDefinition..ctor(DbProviderFactory storeProviderFactory, DbCommandTree commandTree) +1267 
    System.Data.EntityClient.EntityProviderServices.CreateCommandDefinition(DbProviderFactory storeProviderFactory, DbCommandTree commandTree) +97 
    System.Data.EntityClient.EntityProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree) +198 
    System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree) +147 
    System.Data.Objects.Internal.ObjectQueryExecutionPlan.Prepare(ObjectContext context, DbQueryCommandTree tree, Type elementType, MergeOption mergeOption, Span span, ReadOnlyCollection`1 compiledQueryParameters) +371 
    System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption) +642 
    System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) +149 
    System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +44 
    System.Data.Entity.Internal.Linq.InternalQuery`1.GetEnumerator() +40 
    System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() +40 
    System.Linq.Buffer`1..ctor(IEnumerable`1 source) +205 
    System.Linq.Enumerable.ToArray(IEnumerable`1 source) +78 
    BseWms.WebUI.Models.Reports.GetAllTransaction(TransactionSearchModel searchModel) in C:\Users\mark\Documents\Visual Studio 2010\Projects\BseWms\BseWms.WebUI\Models\Reports.cs:149 
    BseWms.WebUI.Controllers.ReportsController.GetAllTransaction(TransactionSearchModel searchModel) in C:\Users\mark\Documents\Visual Studio 2010\Projects\BseWms\BseWms.WebUI\Controllers\ReportsController.cs:187 
    lambda_method(Closure , ControllerBase , Object[]) +108 
    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 
    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() +8836977 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184 
+0

什麼「不工作」。請明確點。 – 2012-07-09 04:29:22

+0

在你的'result = result.Where(...',我很清楚'x.etaDate'是什麼,它不清楚右邊的'etaDate'是什麼,它來自哪裏或者什麼其值爲: – Tod 2012-07-09 04:39:08

+0

什麼是etaDate – Tilak 2012-07-09 04:40:55

回答

1

所以你確實意識到在你向我們展示的代碼中,etaDate沒有顯示被創建或分配的權利?你在評論中提到過它(除非我只是錯過了它),我認爲這增加了混淆。就我個人而言,我發現爲了不同的目的而提供一個變量名稱,而不是讓人困惑。在您的匿名類型中,您創建了一個名爲etaDate的字段,並將其與我所假設的名爲etaDate的方法的變量進行比較。也許將過濾器重命名爲etaFilterDate會有所幫助。如果沒有別的東西,它肯定會有助於解決這個問題。

嘗試使用調試器並停止Where()子句並檢查結果和etaDate的內容。我懷疑你會發現沒有與etaDate完全匹配的x.etaDate。請記住,日期和時間必須匹配。根據你的評論,你的etaDate將有一個午夜時間,我相信。

0

我假設你要零個結果在第17行?這個比較看起來是正確的,我可以用etaDateetaDate匹配來選出所有結果。所以我猜你的問題是前面的查詢沒有找到與etaDate匹配的記錄 - 你可以仔細檢查一下是否是這種情況?

+0

謝謝你的回答,我試過了,result = result.Where(s = > s.etaDate> =新日期時間(2012,5,20)); 如果(結果= NULL){ 返回 result.ToArray();} ! 別的 { return new string [0]; }。但同樣的錯誤信息發生。 – 2012-07-09 05:09:01