2017-02-25 16 views
0

這是我有史以來第一個使用MongoDB和NoSQL的項目,所以我知道我可能在這裏犯了很多新手的錯誤。好的,首先我會解釋我正在處理的問題陳述。這裏是JSON如何將多個查詢篩選器動態地組合在一個循環中? MongoDB C#

{ 
    "_id" : ObjectId("58b19bb67425e833c4ca4a65"), 
    "features" : [ 
     { 
      "feature" : "Performance", 
      "rating" : -1 
     }, 
     { 
      "feature" : "Speed", 
      "rating" : -2 
     }, 
     { 
      "feature" : "Durability", 
      "rating" : -1 
     } 
    ], 
    "brand" : "EVGA", 
    "stars" : 4, 
    "productId" : "B01H0WU884", 
    "productName" : "EVGA GeForce GTX 1070 SC GAMING ACX 30 8GB GDDR5 LED DX12 OSD Support PXOC Graphics Card 08G-P4-6173-KR", 
    "productDescription" : "The EVGA GeForce GTX 1070 featuring EVGA ACX 3.0 cooling has arrived. This new graphics card features NVIDIA's new \"Pascal\" graphics processor which is the most advanced gaming GPU ever created.", 
    "productCategory" : "Hardware", 
    "productPrice" : "17000", 
    "createdAt" : ISODate("2017-02-25T14:59:02.816Z"), 
    "comments" : [] 
} 

在我的收藏產品的一個文件,並在C#

public class Product 
{ 

    public ObjectId id { get; set; } 
    public string productId { get; set; } 
    public string productName { get; set; } 
    public string productDescription { get; set; } 
    public string productCategory { get; set; } 
    public string productPrice { get; set; } 

    public DateTime createdAt { get; set; } 

    public List<ProductFeature> features; 
    public List<string> comments { get; set; } 
    public string brand; 
    public int stars; 
} 

和類ProductFeature

public class ProductFeature 
{ 
    public string feature { get; set; } 
    public int rating { get; set; } 
} 

Product這裏是一個類別的一個文件在我收藏的JSON中

{ 
    "_id" : ObjectId("58b199627425e81e7c56eff1"), 
    "Features" : [ 
     "Performance", 
     "Speed", 
     "Durability" 
    ], 
    "Name" : "Hardware" 
} 

和類Category在C#

public class Category 
{ 
    public List<string> Features { get; set; } 
    public string Name { get; set; } 
    public ObjectId id { get; set; } 
} 

現在,這是我需要做的。 我有一個名爲interestsList<Category>列表。我需要從集合中選擇來自類別列表中類別的產品,其中我獲得的每個產品都必須至少包含一個評分> 0的功能,並且該功能也存在於附帶的功能列表中類別列表中的每個類別。

我知道這似乎很複雜,這就是爲什麼我需要幫助。下面的代碼雖然不正確,但會幫助你更好地理解。我需要知道如何使這段代碼工作。我已經完全評論了我遇到的問題。

var query = null; // Error: Can't initialize 'var' object to null 
    for (int i=0;i<interests.Count;i++) 
    { 
     var q1 = Query<Controller.Product>.Where(p => p.productCategory == interests[i].Name); 
     var q2 = null; // Error: Can't initialize 'var' object to null 
     for (int j=0;j<interests[i].Features.Count;j++) 
     { 
      // Exception on this next line 
      var q3 = Query<Controller.Product>.Where(p => p.features.All(f => f.feature == interests[i].Features[j] && f.rating > 0)); 
      q2 = Query.Or(q2, q3); 
     } 
     query = Query.Or(query, Query.And(q1, q2)); 

    } 
    var result = collection.Find(query).SetLimit(20); 

這裏是我正在

不支持例外where子句:Enumerable.All(p.features, (ProductFeature F)=>((f.feature == 「性能」 )& &(f.rating> 0)))。

如果我把Any,而不是All在過濾器定義,代碼開始工作,但我需要All工作。 任何幫助表示讚賞。

+0

我會建議用戶'List'。你可以嘗試'var q3 = Query .ToList()。Where(p => p.features.All(f => f.feature == interests [i] .Features [j] && f.rating > 0));' – Venky

回答

1

根據示例,您將需要下面的查詢。

db.product.find({ 
    "productCategory": "Hardware", 
    "features": { 
     "$elemMatch": { 
      "feature": { 
       "$in": ["Performance", 
        "Speed", 
        "Durability" 
       ] 
      }, 
      "rating": { 
       "$gt": 5 
      } 
     } 
    } 
}) 

下面是簡化的C#代碼,讓產品所有類別

var pQuery = Query.Empty; 
for (int i = 0; i < interests.Count; i++) { 
    var cQuery = Query.And(
     Query<Product>.EQ(u => u.productCategory, interests[i].Name), 
     Query<Product>.ElemMatch(f => f.features, e => Query.And(
      Query<ProductFeature>.In(u => u.feature, interests[i].Features), 
      Query<ProductFeature>.GT(u => u.rating, 5)))); 
    pQuery = Query.Or(pQuery, cQuery); 
} 
var result = collection.Find(pQuery).SetLimit(20); 
} 
+0

'pQuery = Query.Or(pQuery,cQuery)'行在您的代碼片段中不起作用。它不會拋出任何異常,但在調試'pQuery = {{}}'時,即使在執行該行之後。當我在調試模式下檢查對象內的包裝器時,它顯示出某種錯誤的轉換異常。我通過做'if(i == 0) {pQuery = cQuery; } else { pQuery = Query.Or(pQuery,cQuery);而不是' }。其餘的工作正常。謝謝。 –

相關問題