[英]How do I use Generics to create multiple/nested SelectMany joins in Linq for DocumentDB
我試圖將方法添加到文檔Db的通用存儲庫中。
我希望它產生看起來與此類似的SQL(但通用):
SELECT VALUE c FROM c
JOIN versions IN c["versions"]
JOIN groups IN versions["userGroups"]
JOIN users IN groups["users"]
WHERE c.type = "Approval" and users["userId"] = "xxx"
我已經嘗試過了...
public async Task<ListResult<TResult>> SelectManyAsync<TEntity1, TEntity2, TEntity3, TEntity4, TResult>(Expression<Func<TEntity1, bool>> condition01, Expression<Func<TEntity1, IEnumerable<TEntity2>>> condition02, Expression<Func<TEntity2, IEnumerable<TEntity3>>> condition03, Expression<Func<TEntity3, IEnumerable<TEntity4>>> condition04, Expression<Func<TEntity4, bool>> condition05) where TEntity1:class
{
var feedOptions = GetFeedOptions();
var query = DocumentDbContext.Client.CreateDocumentQuery<TEntity1>(DocumentDbContext.DocumentCollection.DocumentsLink, feedOptions)
.Where(condition01)
.SelectMany(condition02)
.SelectMany(condition03)
.SelectMany(condition04)
.Where(condition05)
.AsDocumentQuery();
// .. removed code for brevity
}
結果是這樣的:
SELECT VALUE tmp FROM root
JOIN tmp IN root["versions"]
JOIN tmp IN tmp["userGroups"]
WHERE (true AND (tmp["userId"] = "xxx"))
但是我得到了錯誤:
在FROM子句中多次指定了輸入集名稱或別名“ tmp”。
是否可以使用泛型創建類似的內容,以及如何為“ tmp”指定不同的值。 還是這不是正確的方法?
不知道是否該錯誤,此類查詢錯誤將無法工作。 您需要使用嵌套的selectMany。
以下示例應該起作用:
public async Task<IEnumerable<TRes>> Query<T, TRes>(
string docCollection,
string partitionKey,
Func<IOrderedQueryable<T>, IQueryable<TRes>> func,
string continuationToken = null)
{
var queryable =
CosmosClient.CreateDocumentQuery<T>(
CollectionUri(docCollection),
new FeedOptions
{
MaxItemCount = 100,
RequestContinuation = continuationToken,
EnableCrossPartitionQuery = false,
PartitionKey = new PartitionKey(partitionKey),
PopulateQueryMetrics = true,
});
return await func(queryable).AsDocumentQuery().ExecuteNextAsync<TRes>();
}
await Query<Doc, string>("collection", "pk",
(IOrderedQueryable<Doc> x) => x.SelectMany(p => p.Versions.SelectMany(d => d.UserGroups.SelectMany(k => k.Users.Select(u => u.Name)))));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.