0

我有 2 节课,Log并且UserProfile. Log对 有零个或一个引用UserProfile

我正在尝试实现一个过滤器来搜索我的日志。目前它看起来像这样:

    /// <summary>
    /// Searches the logs for matching records
    /// </summary>
    /// <param name="fromUTC">Start point timestamp of the search</param>
    /// <param name="toUTC">End point timestamp of the search</param>
    /// <param name="ofSeverity">Severity level of the log entry</param>
    /// <param name="orHigher">Retrieve more severe log entries as well that match</param>
    /// <param name="sourceStartsWith">The source field starts with these characters</param>
    /// <param name="usernameStartsWith">The username field starts with these characters</param>
    /// <param name="maxRecords">The maximum number of records to return</param>
    /// <returns>A list of Log objects with attached UserProfile objects</returns>
    public IEnumerable<Log> SearchLogs(
        DateTime fromUTC,
        DateTime toUTC,
        string ofSeverity,
        bool orHigher,
        string sourceStartsWith,
        string usernameStartsWith,
        int maxRecords)
    {
        ofSeverity = ofSeverity ?? "INFO";

        var query = DetachedCriteria.For<Log>()
            .SetFetchMode("UserProfile", NHibernate.FetchMode.Eager)
            .Add(Restrictions.In("Severity", (orHigher ?
                Translator.SeverityOrHigher(ofSeverity) : Translator.Severity(ofSeverity)).ToArray()))
            .Add(Restrictions.Between("TimeStamp", fromUTC, toUTC))
            .AddOrder(Order.Desc("TimeStamp"))
            .SetMaxResults(maxRecords);

        if ((sourceStartsWith ?? string.Empty).Length > 0)
        {
            query
                .Add(Restrictions.InsensitiveLike("Source", sourceStartsWith, MatchMode.Start));
        }

        if ((usernameStartsWith ?? string.Empty).Length > 0)
        {
            query
                .Add(Restrictions.InsensitiveLike("UserProfile.UserName",
                    usernameStartsWith, MatchMode.Start));
        }

        return query.GetExecutableCriteria(_Session).List<Log>();
    }

...只要我不指定usernameStartsWith值,它就可以正常工作。

如果我确实指定了一个usernameStartsWith值,我会得到一个可爱的黄色死亡屏幕,上面写着:

could not resolve property: UserProfile.UserName of: C3.DataModel.Log

我已经尝试了所有我能想到的排列来让它工作,但我做不到。有人可以告诉我我做错了什么吗?

4

1 回答 1

1

我知道您说过您已经尝试了一些事情,但是您是否尝试过使用调用CreateCriteria而不是加入 UserProfiles SetFetchMode?也许是这样的:

    if ((usernameStartsWith ?? string.Empty).Length > 0)
    {
        query.CreateCriteria("UserProfile")
             .Add(Restrictions.InsensitiveLike("UserName",
                usernameStartsWith, MatchMode.Start));
    }
于 2012-04-21T21:17:44.587 回答