2

我知道这个问题的变体已经在 SO 上令人作呕,但似乎我的代码已经与被反复标记为答案的内容相匹配。我正在处理一些报告,并已将这种模式用于许多其他类似的报告而没有问题。我有一个这样的控制器动作:

public ActionResult Filter(ChecksClearingFilterVM Filter)
        {
            var Result = DataQuery(Filter).Select(a => new ChecksClearingResultVM
            {
                CheckAmount = a.Amount,
                CheckNumber = a.CheckNumber,
                Payee = a.NpoName,
                CheckClearDate = a.CheckClearDate,
                CheckDate = a.CheckDate,
            }).ToList(); 
            Filter.Result = Result;
            return View("ChecksClearingResult", Filter);
        }

这是有问题的视图模型:

public class ChecksClearingResultVM
    {
        public int? CheckNumber { get; set; }
        public string Payee { get; set; }
        public DateTime CheckDate { get; set; }
        public DateTime? CheckClearDate { get; set; }       
        public decimal CheckAmount { get; set; }       
    }

而另一个视图模型:

public class ChecksClearingFilterVM
    {
        public int BankAccountId { get; set; }
        public SelectList SelectBankAccount { get; set; }
        public DateTime? StartDate { get; set; }
        public DateTime? EndDate { get; set; }
        public IEnumerable<ChecksClearingResultVM> Result { get; set; }
    }

我似乎没有调用任何构造函数,更不用说带参数的构造函数了。它轰炸投影。我特别困惑,因为我有一些报告在相同的设计下工作得很好。

也许我的问题是 DataQuery 函数的问题?这是它的外观:

 public IQueryable<DistroPayment> DataQuery(ChecksClearingFilterVM Filter)
        {
            var wootz = Tds.DistroPayments
                .Where(b =>
                   b.BankAccountId == Filter.BankAccountId
                   && b.CheckClearDate.HasValue
                   || !b.CheckNumber.HasValue);

            if (Filter.StartDate.HasValue || Filter.StartDate.HasValue)
            {
                if (Filter.StartDate.HasValue)
                {
                    wootz = wootz.Where(x => x.CheckClearDate >= Filter.StartDate);
                }

                if (Filter.EndDate.HasValue)
                {
                    wootz = wootz.Where(x => x.CheckClearDate <= Filter.EndDate);
                }
            }
            else
            {
                wootz = wootz.Where(x => x.CheckClearDate >= new DateTime(DateTime.Now.Year, 1, 1));
            }

            return wootz;
        }

根据要求,这是我的堆栈跟踪。准备 Wall-O-Text:

<!--

Message: Only parameterless constructors and initializers are supported in LINQ to Entities.
Source: System.Data.Entity
StackTrace:
   at System.Data.Objects.ELinq.ExpressionConverter.NewTranslator.TypedTranslate(ExpressionConverter parent, NewExpression linq)
   at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
   at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
   at System.Data.Objects.ELinq.ExpressionConverter.UnaryTranslator.TypedTranslate(ExpressionConverter parent, UnaryExpression linq)
   at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
   at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
   at System.Data.Objects.ELinq.ExpressionConverter.BinaryTranslator.TypedTranslate(ExpressionConverter parent, BinaryExpression linq)
   at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
   at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
   at System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input)
   at System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input, DbExpressionBinding& binding)
   at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda)
   at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
   at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod)
   at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
   at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
   at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
   at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda)
   at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SelectTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
   at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod)
   at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
   at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
   at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
   at System.Data.Objects.ELinq.ExpressionConverter.Convert()
   at System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
   at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
   at System.Data.Entity.Internal.Linq.InternalQuery`1.GetEnumerator()
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at TDS.UI.Controllers.ChecksClearingController.Filter(ChecksClearingFilterVM Filter) in C:\Users\gbisaccia.CREATEHOPE\Documents\Visual Studio 2010\Projects\TruistConnect\DEVELOPMENT\TruistConnect\TDS.UI\Controllers\ChecksClearingController.cs:line 59
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)

-->
4

1 回答 1

3
wootz = wootz.Where(x => x.CheckClearDate >= new DateTime(DateTime.Now.Year, 1, 1));

那看起来不对。实体框架不理解这个构造函数。您应该能够通过将其设为参数来修复此部分:

var startOfYear = new DateTime(DateTime.Now.Year, 1, 1);
wootz = wootz.Where(x => x.CheckClearDate >= startOfYear);

搜索也可以EntityFunctions.CreateDateTime作为替代方案。

(注意:我没有检查这是否是唯一的问题。)

于 2012-08-16T20:36:29.307 回答