2013-10-30 28 views
1

基本上我試圖用數據庫中的數據生成報告。但是每次我生成一個報告,我得到這個異常:我使用QueryOver錯誤嗎?

variable 'x' of type 'HealthCardLabelPrinter.Entities.CardProcessed' referenced from scope '', but it is not defined

我的代碼拋出異常是在這裏:

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) 

回答

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 
+0

也做到了,感謝您的簡要說明。 – Craig