Select或SelectList:我要包括整个实体 [英] Select or SelectList: I Want to include the entire entity

查看:105
本文介绍了Select或SelectList:我要包括整个实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要查询对象列表,然后将它们与子查询相关联.

I am querying a list of objects, and then correlating them with a subquery.

我想返回子查询以及根实体的结果.但是

I want to return the results of the subquery, as well as the root entity. But

我不知道如何实际返回根实体,只能返回其单个属性.

I can't figure out how to actually return the root entity, I can only return individual properties of it.

具体来说,这可行:

this.Session.QueryOver<MediaFile>(() => mediaFile)
            .SelectList(list => list.Select(mf => mf.Id));

但这不是:

this.Session.QueryOver<MediaFile>(() => mediaFile)
            .SelectList(list => list.Select(mf => mf));

我收到以下错误:

无法解析MediaFile的属性:

Could not resolve property : of MediaFile

有人知道我如何将实体作为属性包括在列表中吗?

Does anyone know how I can include the entity as a property in the list?

这是我完整的示例:

// My target class
private class MediaFileAndCount
{
    // I can successfully populate these fields.
    public long MediaFileId { get; set; }
    public int DistinctPlaylistCount { get;set; }

    // But I want to populate this field!
    public MediaFile MediaFile { get; set; }
}


private void TrySingleQuery()
{
    MediaFile mediaFile = null;
    PlaylistEntry playlistEntry = null;

    MediaFileAndCount mfc = null;
    var subQuery = QueryOver.Of<PlaylistEntry>(() => playlistEntry)
        .Where(() => playlistEntry.MediaFile.Id == mediaFile.Id)
        .Select(Projections.CountDistinct<PlaylistEntry>(p => p.Playlist));


    var query = this.Session.QueryOver<MediaFile>(() => mediaFile)
        .SelectList(list => list
            .Select(mf => mf.Id).WithAlias(() => mfc.MediaFileId)
            .Select(Projections.SubQuery(subQuery)).WithAlias(() => mfc.DistinctPlaylistCount)
     // .Select(mf => mf).WithAlias(() => mfc.MediaFile)  // This line fails
        )
        .TransformUsing(Transformers.AliasToBean<MediaFileAndCount>());


    var results = query.List<MediaFileAndCount>();
}

推荐答案

另一种查询方式

var allMediaFiles = session.QueryOver<MediaFile>().Where(...).ToFuture(); // just get the MediaFiles into sessionCache

var results = session.QueryOver<PlaylistEntry>()
    .Where(p => p.MediaFile...)
    .SelectList(list => list
        .GroupBy(p => p.MediaFile.Id)
        .CountDistinct(p => p.Playlist))
    .ToFuture<object[]>()
    .Select(a => new MediaFileAndCount
    {
        MediaFile = session.Get<MediaFile>((long)a[0]),
        DistinctPlaylistCount = (int)a[1]
    })
    .ToList();

foreach (var mediaFile in allMediaFiles.Except(results.Select(r => r.MediaFile)))
{
    results.Add(new MediaFileAndCount { MediaFile = mediaFile });
}

这篇关于Select或SelectList:我要包括整个实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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