我目前正在开发 net core 2 应用程序。
我创建了一个“商品”抽象类。
public abstract class Good : BaseEntity
{
public int Id { get; set; }
public string Title { get; set; }
public string Details { get; set; }
public double Price { get; set; }
public string IndexId { get; set; }
public string RefPintel { get; set; }
public bool IsDiscountable { get; set; }
public int ProductTypeId { get; set; }
public bool IsEnabled { get; set; }
public int DepartmentId { get; set; }
public string Season { get; set; }
}
两个继承类 Product 和 Batch。
public class Product : Good
{
public string File { get; set; }
public bool? IsDisplayedOnJCE { get; set; }
public bool? IsBasicProduct { get; set; }
public int? PintelSheetId { get; set; }
public class Batch : Good
{
public ICollection<Product> Products { get; set; }
如您所见,一个批次包含一组产品。
一切正常,EF 使用 TPH 继承模型创建了一个单表“商品”。批次中的产品具有 BatchId(由 EF 创建的列),鉴别器列运行良好(即使我无法在我的代码中访问它)等等......
我的问题是,当我执行 GetAll 请求时,我会两次获得所有批次产品。
{
"products": [
{
**"id": 85,**
"title": "500 Pièces LEGO",
"details": "Boîte de 500 pièces LEGO pour construire des trucs mega top",
"refPintel": "0002",
"file": "lego-500pcs.pdf",
"providerId": 2,
"productType": "Jouet"
},
{
"id": 86,
"title": "1000 Pièces LEGO",
"details": "Boîte de 1000 pièces LEGO pour construire des trucs mega top",
"refPintel": "0001",
"file": "lego-1000pcs.pdf",
"providerId": 3,
"productType": "Jouet"
}
],
"id": 78,
"details": "Lot super LEGO 1500 pièces ! ",
"title": "Lot LEGO TOP WOWOW",
"price": 139.99,
"indexId": "J",
"refPintel": "0004",
"isDiscountable": true,
"productTypeId": 1,
"isEnabled": true,
"departmentId": 1,
"season": "2018",
"isBatch": true,
"createdBy": "mathieu",
"updatedBy": "mathieu"
},
{
"file": "lego-500pcs.pdf",
"isDisplayedOnJCE": true,
"isBasicProduct": true,
"pintelSheetId": 0,
"providerId": 2,
"id": 85,
"details": "Boîte de 500 pièces LEGO pour construire des trucs mega top",
"title": "500 Pièces LEGO",
"price": 50,
"indexId": "G",
"refPintel": "0002",
"isDiscountable": false,
"productTypeId": 1,
"isEnabled": true,
"departmentId": 1,
"season": null,
"isBatch": false,
"createdBy": "mathieu",
"updatedBy": "mathieu"
},
{
"file": "lego-1000pcs.pdf",
"isDisplayedOnJCE": true,
"isBasicProduct": true,
"pintelSheetId": 0,
"providerId": 3,
"id": 86,
"details": "Boîte de 1000 pièces LEGO pour construire des trucs mega top",
"title": "1000 Pièces LEGO",
"price": 100,
"indexId": "I",
"refPintel": "0001",
"isDiscountable": false,
"productTypeId": 1,
"isEnabled": true,
"departmentId": 1,
"season": null,
"isBatch": false,
"createdBy": "mathieu",
"updatedBy": "mathieu"
},
如您所见,我有两个产品(id 85 和 id 86)包含在我的批次中并包含在我的“商品”查询中。它们是相同的,它们不应该存在于批次之外。
我设法不使用 foreach 从我的商品查询中删除它们
foreach (var item in query)
{
if (item is Batch)
{
var batch = item as Batch;
query = query.Except(query.Where(p => batch.Products.Any(b => b.Id == p.Id)));
}
}
结果
{
"products": [
{
"id": 85,
"title": "500 Pièces LEGO",
"details": "Boîte de 500 pièces LEGO pour construire des trucs mega top",
"refPintel": "0002",
"file": "lego-500pcs.pdf",
"providerId": 2,
"productType": "Jouet"
},
{
"id": 86,
"title": "1000 Pièces LEGO",
"details": "Boîte de 1000 pièces LEGO pour construire des trucs mega top",
"refPintel": "0001",
"file": "lego-1000pcs.pdf",
"providerId": 3,
"productType": "Jouet"
}
],
"id": 78,
"details": "Lot super LEGO 1500 pièces ! ",
"title": "Lot LEGO TOP WOWOW",
"price": 139.99,
"indexId": "J",
"refPintel": "0004",
"isDiscountable": true,
"productTypeId": 1,
"isEnabled": true,
"departmentId": 1,
"season": "2018",
"isBatch": true,
"createdBy": "mathieu",
"updatedBy": "mathieu"
},
{
"products": [
{
"id": 88,
"title": "Puzzle 3d 850 pieces - poudlard la grande salle",
"details": "Édifiez la célèbre École de Magie et de Sorcellerie avec Poudlard - Grande Salle, un puzzle 3D en mousse de 850 pièces. Vous y retrouverez plusieurs lieux connus du merveilleux monde d'Harry Potter tel que la grande salle, le bureau du directeur ainsi que plusieurs autres. Vous pouvez combiner ce puzzle avec Poudlard - Tour d'astronomie (référence 04031480 vendue séparément) et obtenir un puzzle 3D de 1725 pièces. Livret d'instructions inclus. Assemblage à tenons et mortaises, produisant des structures et assemblages 3D des plus solides. Conception et reproductions d'illustrations de qualité supérieure. Pour des heures de divertissement et de plaisir en famille. Dim. : L.50 x l.38 x H.47 cm.",
"refPintel": "0003",
"file": "puzzle-Poudlards.pdf",
"providerId": 2,
"productType": "Jouet"
}
],
"id": 87,
"details": "lot de produits harry potter",
"title": "Lot Harry Potter",
"price": 199.9,
"indexId": "J",
"refPintel": "0005",
"isDiscountable": true,
"productTypeId": 1,
"isEnabled": true,
"departmentId": 3,
"season": "2018",
"isBatch": true,
"createdBy": "mathieu",
"updatedBy": "mathieu"
},
这按预期工作,我的产品 85 和 86 没有显示。但正如您可能知道的那样,foreach 正在影响表演。使用 foreach,在 1200 行的表上获得结果大约需要 10 秒。没有,0.2 秒。
我的问题是如何向我的用户发送我的所有商品,即产品和批次,而不会将属于批次的产品发送两次(仅在批次内,不像批次外的产品),而不“过滤”我的查询有一个foreach。
我已经尝试过其他一些事情,比如在我的模型构建器周围玩等等......但没有成功......
也许我在那里遗漏了一些明显的东西,因为我是新手,但我目前遇到了这个性能问题。
如果您需要任何澄清、详细信息或其他任何内容,请告诉我。
谢谢你读我。
编辑:添加了一个数据库屏幕截图,以显示它是否有助于理解问题。
编辑 2:添加了 foreach 的输出。
编辑3:编辑了问题......有点。