问题描述:
Manufacturers
每个包含一个
ManufacturerId
Products
,其中包含
ProductId
以及嵌套的列表
Qualities
QualityId
还有一个
Price
.
--Manufacturers (List)
----ManufacturerId (string)
----Products (List)
------ProductId (string)
------Qualities (List)
-QualityId (string)
-Price (decimal)
或者
class Manufacturer { string ManufacturerId { get; set; } IEnumerable<Product> Products { get; set; } }
class Product { string ProductId { get; set; } IEnumerable<Quality> Qualities { get; set; } }
class Quality { string QualityId { get; set; } decimal Price { get; set; } }
我的目标:
List<string>
其中每个项都是由
$"{ManufacturerId}-{ProductId}-{QualityId}"
钥匙。
如何选择制造商和产品:
-
string[] ProductPriotizeList
=> new[] { "P1", "P8", "P5", "P2" ... };
-
以最便宜的价格订购
质量ID
.
-
构造
钥匙
被选中的
质量ID
我创建了一个名为
FlattenQuality
包含
Quality
属性并添加了
制造商ID
以及
class FlattenQuality
{
string QualityId { get; set; }
string ManufacturerId { get; set; }
string ProductId { get; set; }
decimal Price { get; set; }
string Key { get { return $"{ManufacturerId}-{ProductId}-{QualityId}"; }
}
然后构建了一个
扁平化质量
通过
SelectMany
超过制造商和
在产品上:
List<FlattenQuality> flattenQualities = BuildFlattenQuality(..);
List<string> selected = flattenQualities.OrderBy(x => Array.IndexOf(ProductPriotizeList, x.ProductId)).ThenBy(x => x.Price).Select(x => x.MyKey).ToList();
例如:
{
"Manufacturers": [
{
"ManufacturerId": 1,
"Products": [
{
"ProductId": P1,
"Qualities": [
{
"QualityId": 1,
"Price": 10
},
{
"QualityId": 2,
"Price": 20
}]
}]
},
{
"ManufacturerId": 2,
"Products": [
{
"ProductId": P1,
"Qualities": [
{
"QualityId": 1,
"Price": 15
},
{
"QualityId": 2,
"Price": 30
}]
}]
}]
}
{
"Keys": [
"1-1-1", //The QualityId 1 of productId 1 of ManuId 1 is the cheapest
"2-1-1" //The cheapest sibling
]
}
我的问题:
如何在不污染我的应用程序的情况下获得同样的结果?不用再重复所有的列表。