在不久的将来,我想从 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.