NHibernate 4 upgrade - Cannot simultaneously fetch multiple bags

ε祈祈猫儿з 提交于 2019-12-06 00:26:54

probably

var IdsList = new List { /* Some Ids */ };
var results = session.Query<A>()
    .FetchMany(x => x.B_ObjectsList)
    .Where(x=>IdsList.Contains(x.Id))
    .ToList();

// initialize C_ObjectsList
var bIds = results.SelectMany(x => x.B_ObjectsList).Select(b => b.Id).Distinct().ToList();
session.Query<B>()
    .FetchMany(x => x.C_ObjectsList)
    .Where(b => bIds.Contains(b.Id))
    .ToList();

return results;

If B has a reference to A you can do:

var IdsList = new List { /* Some Ids */ };
var results = session.Query<A>()
                     .Fetch(a => a.B_ObjectsList)
                     .Where(a => IdsList.Contains(a.Id))
                     .ToList();

// initialize C_ObjectsList
var aQuery = session.Query<A>()
                    .Where(x => IdsList.Contains(x.Id));

session.Query<B>()
       .Fetch(b => b.C_ObjectsList)
       .Where(b => aQuery.Contains(b.A)
       .Prefetch();

This has the advantage of not being limited by the max parameters of the DB, which in SQL server defaults to 2100. Instead of ToList(), I use this extension method:

static public void Prefetch<T>(this IQueryable<T> query)
{
    // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
    query.AsEnumerable().FirstOrDefault();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!