使用.NET中的csharp驱动程序对嵌套集合进行MongoDB Projection

时间:2016-02-01 13:38:36

标签: c# .net mongodb mongodb-.net-driver mongodb-csharp-2.0

在不久的将来,我想从SQL Server迁移到MongoDB。现在,我只是在玩各种可能性并测试最佳的架构设计。我偶然发现了一个问题。让我总结一下这个问题。

我有30个制造商,每个制造商都包含尺寸范围列表(如S - > XXL)。每个尺寸范围都有尺寸列表(如S,M,L,XL,XXL)。所以这就是我创造的课程:

public class Manufacturer : BaseEntity
{
    #region Ctor

    public Manufacturer()
    {
        SizeRanges = new List<SizeRange>();           
    }

    #endregion

    #region Properties    

    public string Name { get; set; }
    public string Description { get; set; }
    public string MetaTitle { get; set; }
    public string MetaDescription { get; set; }
    public double PurchaseDiscountPercentage { get; set; }
    public bool LimitedToStores { get; set; }
    public int DisplayOrder { get; set; }
    public State State { get; set; }
    public bool SyncToShop { get; set; }
    public bool Deleted { get; set; }
    public bool ManufactuerTierPriceHasChanged { get; set; }
    public bool ManufactuerSizesHasChanged { get; set; }
    public bool PurchaseDiscountPercentageChanged { get; set; }
    public DateTime? DateChanged { get; set; }
    public DateTime? DateCreated { get; set; }

    #endregion

    #region Mapping

    public Picture Picture { get; set; }
    public List<SizeRange> SizeRanges { get; set; }       

    #endregion

    #region Classes

    public class SizeRange
    {
        // Ctor

        public SizeRange()
        {
            Sizes = new List<Size>();
        }

        // Properties      

        public string Name { get; set; }
        public int DisplayOrder { get; set; }
        public bool Deleted { get; set; }
        public DateTime? DateChanged { get; set; }
        public DateTime? DateCreated { get; set; }

        // Mapping

        public List<Size> Sizes { get; set; }
    }

    public class Size
    {
        // Properties           

        public string SizeName { get; set; }
        public string LookupSize { get; set; }
        public int DisplayOrder { get; set; }
        public bool Deleted { get; set; }
        public DateTime? DateChanged { get; set; }
        public DateTime? DateCreated { get; set; }
    }      

    #endregion
}

现在这是我的问题。 获取所有大小范围的最佳方法是什么?我使用mongoDB 3.2编写了这段代码。我在这里使用了2个步骤,但认为有更好的方法可以从所有制造商中获取所有大小范围。如果有更好的方法,请告诉我。

var sizeRanges = await erpContext
            .Manufacturers
            .Find(FilterDefinition<MongoManufacturer>.Empty)
            .Project(x=> x.SizeRanges)
            .ToListAsync(); // results in a list containing 30 collections of size ranges

var sizeRangesList = sizeRanges
            .SelectMany(x => x)
            .ToList(); // get a list with only the size ranges.

0 个答案:

没有答案