1

基本上我正在尝试使用数据库中的数据生成报告。但是,每次生成报告时,都会出现以下异常:

从范围“”引用的“HealthCardLabelPrinter.Entities.CardProcessed”类型的变量“x”,但未定义

我抛出异常的代码在这里:

 public BindingList<CardProcessed> GetByLocationAndDate(string location_id, string sdate, string edate)
        {
            var fromDb = session.QueryOver<CardProcessed>()
                .Where(x => (DateTime.Parse(x.draw_date) >= DateTime.Parse(sdate)))
                .And(x => DateTime.Parse(x.draw_date) <= DateTime.Parse(edate))
                .JoinQueryOver(m => m._location)
                .Where(m => m.location_id == location_id)
                .List<CardProcessed>();
            var newList = new BindingList<CardProcessed>();
            foreach (var record in fromDb)
            {
                newList.Add(record);
            }
            return newList;
        }

两个类图:

public LocationMap()
    {
        Id(x => x.LocationId).Column("locationId").GeneratedBy.Identity();
        Map(x => x.name)
            .Not.Nullable();
        Map(x => x.location_id)
            .Not.Nullable();
        HasMany(x => x.records)
            .KeyColumns
            .Add("cardProcessedId")
            .ForeignKeyConstraintName("none");
    }

public CardProcessedMap()
    {
        Id(x => x.CardProcessedId).Column("cardProcessedId").GeneratedBy.Identity();
        References<hcData>(x => x._hcData, "dataId").ForeignKey("none");
        References<User>(x => x._user, "userId").ForeignKey("none");
        References<Location>(x => x._location, "locationId").ForeignKey("none");
        References<Doctor>(x => x._doctor, "doctorId").ForeignKey("none");
        Map(x => x.label_amount_encrypted)
            .Column("labelAmount");
        Map(x => x.draw_date)
            .Column("drawDate");
        Map(x => x.fee_encrypted)
            .Column("fee");
        Map(x => x.phone_encrypted)
            .Column("phone");
    }

帮我弄清楚我应该如何为 QueryOver 定义变量“x”。

错误的堆栈跟踪:

at System.Linq.Expressions.Compiler.VariableBinder.Reference(ParameterExpression node, VariableStorageKind storage)
   at System.Linq.Expressions.Compiler.VariableBinder.VisitParameter(ParameterExpression node)
   at System.Linq.Expressions.ParameterExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at System.Linq.Expressions.ExpressionVisitor.VisitMember(MemberExpression node)
   at System.Linq.Expressions.MemberExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes)
   at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at System.Linq.Expressions.ExpressionVisitor.Visit(ReadOnlyCollection`1 nodes)
   at System.Linq.Expressions.Compiler.VariableBinder.VisitLambda[T](Expression`1 node)
   at System.Linq.Expressions.Expression`1.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, DebugInfoGenerator debugInfoGenerator)
   at System.Linq.Expressions.LambdaExpression.Compile()
   at NHibernate.Impl.ExpressionProcessor.FindValue(Expression expression)
   at NHibernate.Impl.ExpressionProcessor.FindMemberProjection(Expression expression)
   at NHibernate.Impl.ExpressionProcessor.ProcessSimpleExpression(Expression left, Expression right, ExpressionType nodeType)
   at NHibernate.Impl.ExpressionProcessor.ProcessSimpleExpression(BinaryExpression be)
   at NHibernate.Impl.ExpressionProcessor.ProcessBinaryExpression(BinaryExpression expression)
   at NHibernate.Impl.ExpressionProcessor.ProcessExpression(Expression expression)
   at NHibernate.Impl.ExpressionProcessor.ProcessLambdaExpression(LambdaExpression expression)
   at NHibernate.Impl.ExpressionProcessor.ProcessExpression[T](Expression`1 expression)
   at NHibernate.Criterion.QueryOver`2.Add(Expression`1 expression)
   at NHibernate.Criterion.QueryOver`2.Where(Expression`1 expression)
   at NHibernate.Criterion.QueryOver`2.NHibernate.IQueryOver<TRoot,TSubType>.Where(Expression`1 expression)
   at HealthCardLabelPrinter.Repositories.CardProcessedRepository.GetAllByLocationAndDate(String location_id, String sdate, String edate) in c:\Users\Craig_2\Documents\Visual Studio 2012\Projects\HealthCardLabelPrinter\HealthCardLabelPrinter\Repositories\CardProcessedRepository.cs:line 52
   at HealthCardLabelPrinter.Report.RenderReport(DateTime start, DateTime end, Location location, String fpath) in c:\Users\Craig_2\Documents\Visual Studio 2012\Projects\HealthCardLabelPrinter\HealthCardLabelPrinter\Report.cs:line 39
   at HealthCardLabelPrinter.frmMain.frmMain_FormClosing(Object sender, FormClosingEventArgs e) in c:\Users\Craig_2\Documents\Visual Studio 2012\Projects\HealthCardLabelPrinter\HealthCardLabelPrinter\frmMain.cs:line 452
   at System.Windows.Forms.Form.OnFormClosing(FormClosingEventArgs e)
   at System.Windows.Forms.Form.CheckCloseDialog(Boolean closingOnly)
4

1 回答 1

1

这里的重点是将 转换Expression为有效的SQL 语句。该.Where()方法期望Expression<Func<CardProcessed>>传入,并尝试将其转换为本机 SQL 语句(在这种情况下不成功)

如果我理解清楚,表中的列包含string,应该比较为DateTime。因此我们可以将过滤条件转换为原生 SQL CAST 语句,并以这种方式比较两个日期:

...
.Where(Restrictions.Le(
    Projections.Cast(NHibernateUtil.DateTime, Projections.Property("draw_date"))
   , sdate)
)
...

这将生成如下 SQL 语句:

WHERE cast( this_.drawDate as DATETIME) <= '2013-10-30' // expecting sdate is today
于 2013-10-30T13:58:55.537 回答