I am trying to query my collections, but I'm not sure how to make an "append" from the genus Query.And()
Here is my domain model for creating the Item document:
public class Item { public ObjectId Id { get; set; } public string ItemTypeTemplate { get; set; } public string UsernameOwner { get; set; } public IList<ItemAttribute> Attributes { get; set; } }
The IList<ItemAttribute> changes depending on the ItemTypeTemplate (a kind of search key to a predefined list of element attributes)
Here is a sample Item document:
{ "_id" : ObjectId("5130f9a677e23b11503fee72"), "ItemTypeTemplate" : "Tablet Screens", //can be other types like "Batteries", etc. //which would change the attributes list and values "UsernameOwner" : "user032186511", "Attributes" : [{ "AttributeName" : "Screen Size", "AttributeValue" : "10.1" }, { "AttributeName" : "Pixel Density", "AttributeValue" : "340" }] }
PROBLEM
Given the "dynamic" nature of IList<ItemAttribute> , I cannot manually specify additional query conditions for AttributeName and AttributeValue , so I thought about using a loop to build the query:
QueryBuilder<Item> qbAttributes = new QueryBuilder<Item>(); foreach (var attribute in item.Attributes) { qbAttributes.And( Query.EQ("Attributes.AttributeName", attribute.AttributeName), Query.EQ("Attributes.AttributeValue", attribute.AttributeValue), ); } var query = Query.And( Query.EQ("TemplateId", item.TemplateId), Query.NE("UsernameOwner", item.UsernameOwner) ); return DBContext.GetCollection<Item>("Items").Find(query).AsQueryable();
How do I "add" qbAttributes to a query ? I tried qbAttributes.And(query); errors qbAttributes.And(query); , but .Find(query) with an invalid argument.
I need something like:
var query = Query.And( Query.EQ("ItemTypeTemplate", item.ItemTypeTemplate), //Tablet Screens Query.NE("UsernameOwner", item.UsernameOwner) //current user // this part is generated by the loop Query.EQ("Attributes.AttributeName", "Screen Size"), Query.EQ("Attributes.AttributeValue", "10.1"), Query.EQ("Attributes.AttributeName", "Pixel Density"), Query.EQ("Attributes.AttributeValue", "340") );