如何做一个QueryOver NHibernate的儿童收集 [英] How to do a QueryOver in Nhibernate on child collection

查看:127
本文介绍了如何做一个QueryOver NHibernate的儿童收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一类名为声明这是该用户的子类。

Hello I have one Class named Notifications which is a child class for the User.

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string UserName { get; set; }
    public ICollection<UserNotification> UserNotifications { get; set; }
}

public class Notification
{
    public int Id { get; set; }
    public ICollection<UserNotification> UserNotifications { get; set; }
    public string Title { get; set; }
    public string Message { get; set; }
    public bool IsRead { get; set; }
    public DateTime CreatedDate { get; set; }
}

public class UserNotification
{
    public User User { get; set; }
    public Notification Notification { get; set; }
}

现在我想要得到用户通过ID这将对所有的通知对于当前用户

Now I want to get the User By ID which will bring all the notifications for the current user.

var user = NhSession.Get<User>(userId);



但我不想让所有的通知。我只是想获得与未读通知用户,只是想获得前5名(最新)通知的用户。

我试图joinQueryOver来实现这一点,但我没能做到这一点。任何人都可以请建议得到这个工作。

I tried to achieve that by joinQueryOver but I was not able to do that. Can anyone please suggest to get this working.

推荐答案

根据最新的更新和新实体(IES)的结构,我们现在可以从配对对象,并快速选择用户利益有未读Notificaitons这样

Based on the latest update and new Entity(ies) structure, we can now profit from Pairing object, and quickly select Users which has unread Notificaitons like this

var session = NHSession.GetCurrent();
Notification notification = null;
UserNotification pair = null;
User user = null;

var subquery = QueryOver.Of<UserNotification>(() => pair)
    // this will give us access to notification
    // see we can filter only these which are NOT read
    .JoinQueryOver(() => pair.Notification, () => notification)
    // here is the filter
    .Where(() => !notification.IsRead)
    // now the trick to take only related to our user
    .Where(() => pair.User.Id == user.Id)
    // and get the user Id
    .Select(x => pair.User.Id);

var listOfUsers = session.QueryOver<User>(() => user)
    .WithSubquery
        .WhereProperty(() => user.Id)
        .In(subquery)
    // paging
    .Take(10)
    .Skip(10)
    .List<User>();



查找每用户id 5条未读的通知



Find 5 unread notifications per userId

var userId = 1;
var subqueryByUser = QueryOver.Of<UserNotification>(() => pair)
    // now we do not need any kind of a join 
    // just have to filter these pairs related to user
    .Where(() => pair.User.Id == userId)
    // and get the notification Id
    .Select(x => pair.Notification.Id);

var notificationsPerUser = session.QueryOver<Notification>(() => notification)
    .WithSubquery
        .WhereProperty(() => notification.Id)
        .In(subqueryByUser)
    .Where(() => !notification.IsRead)
    // if needed we can order
    // .OrderBy(...
    .Take(5)
    .List<Notification>()

这篇关于如何做一个QueryOver NHibernate的儿童收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆