[英]C# Mongodb. Project Json to strong type after grouping
這是我的查詢:
db.getCollection('design_ideas').aggregate(
{ $match : {'Deals.URL': 'warm-up-with-white'}},
{ $unwind : '$Deals' },
{ $match : {'Deals.URL': 'warm-up-with-white'}},
{$group: {_id : '$Deals.URL', Deals: {$addToSet: '$Deals'}}}
)
C#中的相同查詢:
_mongoDB.GetCollection<DesignCategoryMongoModel>("design_ideas")
.Aggregate()
.Match(BsonDocument.Parse("{'Deals.URL': 'warm-up-with-white'}"))
.Unwind("Deals")
.Match(BsonDocument.Parse("{'Deals.URL': 'warm-up-with-white'}"))
.Group(BsonDocument.Parse("{_id : '$Deals.URL', Deals: {$addToSet: '$Deals'}}"))
.FirstOrDefault();
查詢結果是一個具有以下結構的文檔:
{
_id: 'url',
Deals: [{
DealId: 1,
Name: 'Name',
Products: [...]
...
}]
}
我知道,我總是得到Deals結果數組中唯一的Deal
對象。
我的C#課:
public class Deal
{
public int DealId {get; set;}
public string Name {get; set;}
public IEnumerable<Product> Products {get; set;}
}
我的問題是如何將結果Json
投影到強C#類型,並且是否存在獲取唯一一個Deal對象(而非數組)的方法?
您可以為此使用BsonSerializer
var MyDeals = BsonSerializer.Deserialize<List<Deal>>(aggregationResult);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.