2

Sorry my bad English. I'm new to mongodb and c# driver. I have nested documents. The Products array within the Agency is bound by CategoryId and SubCategoryId with Category and SubCategory collections. Normally, I can join with linq. However, I cannot connect another collection with id in the list.

MongoDb version is 4.0 and C# driver version 2.8.0. According to the following structure: How can I fill in the collections that are defined as BsonIgnore?

For example :

public class Category
{
    [BsonId]
    public string Id { get; set; }
    public string Name { get; set; }
}
public class SubCategory
{
    [BsonId]
    public string Id { get; set; }
    [BsonRepresentation(BsonType.ObjectId)]
    public string CategoryId { get; set; }
    public string Name { get; set; }
}
public class Agency
{
    [BsonId]
    public string Id { get; set; }
    public string Name { get; set; }
    public List<Products> Products { get; set; }
}
public class Products
{
    [BsonId]
    public string Id { get; set; }
    public string CategoryId { get; set; }
    public string SubCategoryId { get; set; }
    public string Name { get; set; }
    [BsonIgnore] public Category Category { get; set; }
    [BsonIgnore] public SubCategory SubCategory { get; set; }
}
List<Category> categories = new List<Category>();
categories.Add(new Category { Id = "1", Name = "Category1"});
categories.Add(new Category { Id = "2", Name = "Category2"});
List<SubCategory> subCategories = new List<SubCategory>();
subCategories.Add(new SubCategory(){Id = "71", Name = "SubCategory1", CategoryId = "1"});
subCategories.Add(new SubCategory(){Id = "81", Name = "SubCategory2", CategoryId = "2"});
List<Products> products = new List<Products>();
products.Add(new Products{Id = "1", Name = "Product1", CategoryId = "1", SubCategoryId = "71"});
products.Add(new Products{Id = "2", Name = "Product2", CategoryId = "2", SubCategoryId = "81"});
products.Add(new Products{Id = "3", Name = "Product3", CategoryId = "1", SubCategoryId = "71"});
products.Add(new Products{Id = "4", Name = "Product4", CategoryId = "2", SubCategoryId = "82"});
List<Agency> agencies =new List<Agency>();
agencies.Add(new Agency{Id = "1001", Name = "Agency1", Products = products});

var query = from a in agencies where a.Id == "1001"
            select new
            {
               Id = a.Id,
               Name = a.Name,
               Products = from p in products.ToList().AsQueryable()
                                   join c in categories on p.CategoryId equals c.Id into cat
                                   join sg in subCategories on p.SubCategoryId equals sg.Id into subCat
                                   select new Products
                                   {
                                       Id = p.Id,
                                       Name = p.Name,
                                       Category = cat.First(),
                                       SubCategory=subCategories.First(),
                                       CategoryId = p.CategoryId,
                                       SubCategoryId = p.SubCategoryId
                                   }
            };

I expect the output: The Agency and its products are also required to have the relevant Category and Subcategory in the Products.

Gökhan Atilgan
  • 77
  • 1
  • 2
  • 9
  • I add some code. I have reviewed your unique information in the link you provided. However, I could not apply my own situation. I'd appreciate it if you could help me again. My Best Regards. – Gökhan Atilgan Mar 28 '19 at 14:31

0 Answers0