【发布时间】:2019-01-29 09:34:22
【问题描述】:
我正在尝试构建一个 mongo 查询,我几乎可以在 Linq 中使用它(那里有 95%),但不幸的是,Linq 提供程序中缺少一个功能来执行相当于 Intersect(coll ).Any() 或用 Mongo 的说法 AnyIn()
我知道,如果我将流利的库与一些构建器一起使用,我可以构建一个过滤器,它会给我一个 AnyIn() - 但我缺少的是如何构建整个查询、初始过滤器、聚合、投影和最后的过滤器?
这是我的 Linq 查询的非常接近的近似值 - 只要我不尝试比较过滤器中的集合成员身份,它就可以完全按照要求工作
public List<MyResult> ListMyResults(Expression<Func<MyResult, bool>> filter, int skip, int take)
{
// Limit to tennants on main Entity being queried
var ents = ApplyDataRestrictions(db.GetCollection<Entity>("entities").AsQueryAble());
var children = db.GetCollection<Child>("children").AsQueryable();
var chickens = db.GetCollection<Chicken>("chickens").AsQueryable();
// Join a couple collections for their counts
var result = from ent in ents
join c in children on ent.Id equals c.EntityId into kids
join ck in chickens on ent.Id equals ck.EntityId into birds
// Project the results into a MyResult
select new MyResult
{
Id = ent.Id,
AProperty = ent.AProperty,
SomeCollection = ent.SomeCollection,
SomeOtherCollectionTagsMaybe = ent.SomeOtherCollectionTagsMaybe,
TotalKids = kids.Count(),
TotalChickens = birds.Count()
};
if(filter != null)
{
// Apply the filter that was built up from criteria on the MyResults data shape
result = result.Where(filter);
}
result = result.Skip(skip).Take(take);
return result.ToList()
}
public IQueryable<Entity> ApplyDataRestrictions(IQueryable<Entity> query)
{
... restrict results to only those with my tennant id ...
}
【问题讨论】: