nHibernate Criteria Query 中的数学运算

发布于 2024-09-04 14:58:28 字数 869 浏览 5 评论 0原文

我在使用 nHibernate 查询时遇到问题。

我有一个存储车辆信息的数据库,用户可以通过品牌、型号、类型和生产日期搜索数据库。

品牌、型号和类型搜索很好,效果很好,这是我遇到的问题的生产日期。所以这里...

日期存储为整数(StartMonth、StartYear、FinishMonth、FinishYear),当最终用户选择日期时,它会作为整数传递给查询,例如 2010006 (2010 * 100 + 6)。

以下是我正在使用的查询的一部分,仅供参考,我正在使用 Lambda 扩展

if (_searchCriteria.ProductionStart > 0)
{
    query.Add<Engine>(e => ((e.StartYear * 100) + e.StartMonth) >= _searchCriteria.ProductionStart);
}

if (_searchCriteria.ProductionEnd > 0)
{
    query.Add<Engine>(e => ((e.FinishYear * 100) + e.FinishMonth) <= _searchCriteria.ProductionEnd);
}

但是当查询运行时我收到以下消息,

无法从 ((e.StartYear * 100) + e.StartMonth) 确定成员

任何帮助都会很棒,

问候

Rich

I am having troubles with a nHibernate query.

I have a db which stores vehicle info, and the user is able to search the db by make, model, type and production dates.

Make, model & type search is fine, works a treat, it is the productions dates I am having issues with. So here goes...

The dates are stored as ints (StartMonth, StartYear, FinishMonth, FinishYear), when the end-user selects a date it is passed to the query as an int eg 2010006 (2010 * 100 + 6).

below is part of the query I am using, FYI I am using Lambda Extensions.

if (_searchCriteria.ProductionStart > 0)
{
    query.Add<Engine>(e => ((e.StartYear * 100) + e.StartMonth) >= _searchCriteria.ProductionStart);
}

if (_searchCriteria.ProductionEnd > 0)
{
    query.Add<Engine>(e => ((e.FinishYear * 100) + e.FinishMonth) <= _searchCriteria.ProductionEnd);
}

But when the query runs I get the following message,

Could not determine member from ((e.StartYear * 100) + e.StartMonth)

Any help would be great,

Regards

Rich

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若沐 2024-09-11 14:58:28

我不太了解这些 linq 扩展,但我假设您需要在操作之前传递成员名称。

int year = Math.Floor(_searchCriteria.ProductionStart / 100);
int month = _searchCriteria.ProductionStart % 100;

query.Add<Engine>(
  (e => e.StartYear >= year);
  || (e => e.StartMonth >= month && e => e.StartYear >= year))

要么年份必须更大,要么年份至少相等且月份更大。这同样适用于低于的 ProductionEnd。

我不确定 lambda 扩展是否支持这样的“或”(||) 运算符,但“或”肯定有可能。


来自理查兹的评论:

query.Add(
  Restrictions.Or( 
    Restrictions.And(
      SqlExpression.CriterionFor<Engine>(e => e.StartMonth >= month),   
      SqlExpression.CriterionFor<Engine>(e => e.StartYear >= year)), 
    SqlExpression.CriterionFor<Engine>(e => e.StartYear >= year) ) );

I don't know these linq extensions very well, but I assume that you need to pass the member name before the operation.

int year = Math.Floor(_searchCriteria.ProductionStart / 100);
int month = _searchCriteria.ProductionStart % 100;

query.Add<Engine>(
  (e => e.StartYear >= year);
  || (e => e.StartMonth >= month && e => e.StartYear >= year))

Either the year must be greater or the year is at least equal and the month is greater. The same applies to ProductionEnd with lower than.

I'm not sure if the lambda extensions support "or" (||) operator like this, but there is surely a possibility for "or".


From Richards comment:

query.Add(
  Restrictions.Or( 
    Restrictions.And(
      SqlExpression.CriterionFor<Engine>(e => e.StartMonth >= month),   
      SqlExpression.CriterionFor<Engine>(e => e.StartYear >= year)), 
    SqlExpression.CriterionFor<Engine>(e => e.StartYear >= year) ) );
静水深流 2024-09-11 14:58:28

@Stefan,我尝试了以下操作,

if (_searchCriteria.ProductionStart > 0)
{
    int sYear = (int) Math.Floor( (double) _searchCriteria.ProductionStart / 100);
    int sMonth = _searchCriteria.ProductionStart % 100;

    query.Add(
        Restrictions.Or(
            Restrictions.And(
                SqlExpression.CriterionFor<Engine>(e => e.StartMonth >= sMonth),
                SqlExpression.CriterionFor<Engine>(e => e.StartYear >= sYear)
            ),
            SqlExpression.CriterionFor<Engine>(e => e.StartYear >= sYear)
        )
    );
}

if (_searchCriteria.ProductionEnd > 0)
{
    int eYear = (int)Math.Floor((double)_searchCriteria.ProductionEnd / 100);
    int eMonth = _searchCriteria.ProductionEnd % 100;

    query.Add(
        Restrictions.Or(
            Restrictions.And(
                SqlExpression.CriterionFor<Engine>(e => e.FinishMonth <= eMonth),
                SqlExpression.CriterionFor<Engine>(e => e.FinishYear <= eYear)
            ),
            SqlExpression.CriterionFor<Engine>(e => e.FinishYear <= eYear)
        )
    );
}

我使用不同的 AND / OR 组合检查结果(即 ((a && b ) || a) 和 (a || (a && b)) )并弹出几条记录。

经过进一步思考,我得到以下结果,

if (_searchCriteria.ProductionStart > 0)
{
    query.Add<Engine>(e => e.StartDate >= _searchCriteria.ProductionStart);
}

if (_searchCriteria.ProductionEnd > 0)
{
    query.Add<Engine>(e => e.FinishDate <= _searchCriteria.ProductionEnd);
}

其中 StartDateFinishDate 是表中的计算列和实体的属性。

当我们的导师下周回来时我可能会编辑这个。

感谢和问候
富有的

@Stefan, I have tried the following,

if (_searchCriteria.ProductionStart > 0)
{
    int sYear = (int) Math.Floor( (double) _searchCriteria.ProductionStart / 100);
    int sMonth = _searchCriteria.ProductionStart % 100;

    query.Add(
        Restrictions.Or(
            Restrictions.And(
                SqlExpression.CriterionFor<Engine>(e => e.StartMonth >= sMonth),
                SqlExpression.CriterionFor<Engine>(e => e.StartYear >= sYear)
            ),
            SqlExpression.CriterionFor<Engine>(e => e.StartYear >= sYear)
        )
    );
}

if (_searchCriteria.ProductionEnd > 0)
{
    int eYear = (int)Math.Floor((double)_searchCriteria.ProductionEnd / 100);
    int eMonth = _searchCriteria.ProductionEnd % 100;

    query.Add(
        Restrictions.Or(
            Restrictions.And(
                SqlExpression.CriterionFor<Engine>(e => e.FinishMonth <= eMonth),
                SqlExpression.CriterionFor<Engine>(e => e.FinishYear <= eYear)
            ),
            SqlExpression.CriterionFor<Engine>(e => e.FinishYear <= eYear)
        )
    );
}

I check the results with the different AND / OR combinations (i.e. ((a && b ) || a) and (a || (a && b)) ) and a couple of records pop through.

After some further thought I have the following,

if (_searchCriteria.ProductionStart > 0)
{
    query.Add<Engine>(e => e.StartDate >= _searchCriteria.ProductionStart);
}

if (_searchCriteria.ProductionEnd > 0)
{
    query.Add<Engine>(e => e.FinishDate <= _searchCriteria.ProductionEnd);
}

Where StartDate and FinishDate are computed columns in my table and properties on my entity.

I may edit this when our guru returns next week.

Thanks and Regards
Rich

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文