linq 基于字段值的不同实体的EF核心查找

lymnna71  于 2024-01-03  发布在  其他
关注(0)|答案(1)|浏览(182)

在我的模型中,我有一个实体,它的字段可以根据另一个字段(在我的示例中是Document_Type)的值连接(LookUp)不同的表。
举例说明:我有采购线实体,它可以与项目或固定资产实体的关系,字段document_type定义哪个实体相关。
采购行实体

  1. public class PurchaseLine
  2. {
  3. public string No { get; set; }
  4. public int Document_Type { get; set; }
  5. public string Document { get; set; } // it's can be item or fixed asset.
  6. }

字符串
物料资产实体

  1. public class Item
  2. {
  3. public string No { get; set; }
  4. // other fields relating to the item table
  5. }


固定资产主体

  1. public class FixedAsset
  2. {
  3. public string No { get; set; }
  4. // other fields relating to the fixed asset table
  5. }


在最终用户界面中,如果文档类型等于0,我需要列出项目,如果它等于1,我需要列出资产,并且可以轻松地访问和在相关表中执行操作。

brqmpdu1

brqmpdu11#

您需要阅读如何使用EF Core。这里有一些链接:
Microsoft Documentation
Tutorial
为了大致回答您的问题,您需要将Item和FixedAsset的IEnumerables添加到ProductLine模型中,以便它可以访问它们。
然后你可以这样做:

  1. var productLineWithItem = await _context.ProductLine
  2. .Where(x => x.Document_Type == 1)
  3. .Include(x => x.Item)
  4. .ToListAsync();

字符串

相关问题