我有兩個集合,一個是影像名稱串列,第二個是該串列的子集。當一個任務完成時,它的名字被插入到第二個集合中。
我需要從第一個集合中檢索一組尚未完成的影像名稱。我通過以下方式成功實作了這一目標:
var processedNames = processed.AsQueryable().Select(x => x.ImageName).ToArray();
foreach (var result in results.Where(x => !processedNames.Contains(x.ImageName))
但是,這會從資料庫中帶回大量字串,然后將其以單個檔案的形式發送回資料庫,而且效率低下最終會中斷。
所以我試圖重寫它,所以它都是在服務器端執行的:
var results = from x in captures
join complete in processed.AsQueryable() on x.ImageName equals complete.ImageName into completed
where !completed.Any()
select x;
這失敗了:
System.NotSupportedException: '$project 或 $group 不支持 {document}。'
我還嘗試使用非 LINQ API:
var xs = capturesCollection.Aggregate()
.Lookup("Processed", "ImageName", "ImageName", @as: "CompletedCaptures")
.Match(x => x["CompletedCaptures"] == null)
.ToList();
這失敗了:
MongoDB.Bson.BsonSerializationException: 'C# null values of type 'BsonValue' cannot be serialized using a serializer of type 'BsonValueSerializer'.'
如何使用 C# 驅動程式完全在服務器端實作此查詢?純 LINQ 解決方案更適合可移植性。
uj5u.com熱心網友回復:
我想出了如何使用AggregateAPI 來做到這一點:
var results = capturesCollection.Aggregate()
.As<CaptureWithCompletions>()
.Lookup(processed, x => x.ImageName, x => x.ImageName, @as:(CaptureWithCompletions x) => x.CompletedCaptures)
.Match(x => !x.CompletedCaptures.Any())
//.Limit(2)
.ToList();
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/434893.html
