简体   繁体   English

NHibernate 5 标准必须使用等效于 LINQ.Contains()

[英]NHibernate 5 Criteria must use equivalent of LINQ .Contains()

We have a NHibernate V5 project were some queries need improvement.我们有一个 NHibernate V5 项目,有些查询需要改进。 We had one query using ICriteria like this:我们有一个使用 ICriteria 的查询,如下所示:

var list = session.CreateCriteria(typeof(ClockEntry))
        .Add(Restrictions.And(
                        Restrictions.Ge("Time", startDate),
                        Restrictions.Le("Time", endDate)
                    ))
                    .SetProjection(Projections.ProjectionList()
                        .Add(Projections.Property("Id"))
                        .Add(Projections.Property("Time"))
                )

.List<IList>()
.Select(ce => new ClockEntry() { Id = (Guid)ce[0], Time = (DateTime)ce[1] });

The last line of code (the Select) creates a new ClockEntry list where ONLY the Id and Time retain values.最后一行代码(Select)创建一个新的 ClockEntry 列表,其中只有 Id 和 Time 保留值。 All other fields (columns) are null.所有其他字段(列)为 null。

We then wrote a new query using NHibernate Query where the "Contains" element was added.然后,我们使用 NHibernate Query 编写了一个新查询,其中添加了“包含”元素。 This is as follows:如下:

    var p = Rules.UserDataFilters.UserPayrollFilterNumbers(user, session, false);
    var s = Rules.UserDataFilters.UserSiteFilterCodes(user, session);

    ClockEntryRepository clockEntryRepository = new ClockEntryRepository(session);
        var r = clockEntryRepository
            .Query()
            .Where(c =>
                c.Time >= startDate &&
                c.Time <= endDate &&
                p.Contains(c.Employee.Payroll.Number) &&
                s.Contains(c.Employee.Site.Code));

Notice the added 'p' and 's' constraints注意添加的“p”和“s”约束

The problem: I am unable to find the correct construct to add the 'Contains' into the first code block and I am unable to find a elegant way in which to achieve the Select statement in the 2nd example.问题:我无法找到将“包含”添加到第一个代码块中的正确构造,并且我无法找到一种优雅的方式来实现第二个示例中的 Select 语句。 Obviously either one will do but I would greatly appreciate input for answers for both scenarios.显然,任何一个都可以,但我非常感谢为这两种情况提供答案。

The Icriteria query "p" and "s" items I managed to resolve once I figured out that in SQL this would be a IN construct so it became:一旦我发现在 SQL 中这将是一个 IN 构造,我设法解决了 Icriteria 查询“p”和“s”项目,因此它变成了:

            var list = session.CreateCriteria(typeof(ClockEntry))
                .Add(Restrictions.Ge("Time", strtDate))
                .Add(Restrictions.Le("Time", entDate))
                .Add(Restrictions.In("PayrollNo", p))
                .Add(Restrictions.In("SiteCode", s))

            .SetProjection(Projections.ProjectionList()
                .Add(Projections.Property("Id"))
                .Add(Projections.Property("Time")))
            .List<IList>()
            .Select(ce => new ClockEntry() { Id = (Guid)ce[0], Time = (DateTime)ce[1] });
            
            return list.ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM