【问题标题】:Linq join on a caseLinq 加入一个案例
【发布时间】:2020-11-14 16:07:20
【问题描述】:

我想根据 ProductType 调用我的加入。

var stocks = (from st in _context.StockDetails
    join p in _context.ProductType1 on p.ProductId equals p.Id
    select new StockList
    {
    ProductId = st.ProductId,
    ProductType = st.ProductType,
    StockValue = st.StockValue,
    InOut = st.InOut,
    ProductName  = p.ProductName 
    });

在这种情况下,st.ProductType = ProductType1 但我对 ProductType2、ProductType3 也有不同的表...并且想加入足够的表。

这些表不相同。他们有自己的特定专栏,也有一些常见的专栏。表 ProductType1 用于书籍,ProductType2 用于园艺工具,...当然 ProductName 是所有人的通用列(还有一些其他列,例如重量,...)但我决定将这些产品拆分到不同的表中,我在这里! !! 这个过程还没有结束。 从战略上讲,将它们放在具有特定名称字段(如 String1、String2、Int1、Int2 ...... 感谢您的帮助。

【问题讨论】:

  • 请添加更多信息。为什么你需要加入几个相同的表?什么是足够的,哪个规则?
  • @SvyatoslavDanyliv,我确实添加了更多信息(和问题)

标签: linq join


【解决方案1】:

根据您的设计,我没有看到很多选择。你必须选择哪个更适合你

解决方案 1

var stocks1 = from st in _context.StockDetails
    join p in _context.ProductType1 on p.ProductId equals p.Id
    where st.ProductType == 1
    select new StockList
    {
        ProductId = st.ProductId,
        ProductType = st.ProductType,
        StockValue = st.StockValue,
        InOut = st.InOut,
        ProductName  = p.ProductName 
    };

var stocks2 = from st in _context.StockDetails
    join p in _context.ProductType2 on p.ProductId equals p.Id
    where st.ProductType == 2
    select new StockList
    {
        ProductId = st.ProductId,
        ProductType = st.ProductType,
        StockValue = st.StockValue,
        InOut = st.InOut,
        ProductName  = p.ProductName 
    };

var stocks3 = from st in _context.StockDetails
    join p in _context.ProductType3 on p.ProductId equals p.Id
    where st.ProductType == 3
    select new StockList
    {
        ProductId = st.ProductId,
        ProductType = st.ProductType,
        StockValue = st.StockValue,
        InOut = st.InOut,
        ProductName  = p.ProductName 
    };

...
var stocks = stocks1.Concat(stocks2).Concat(stocks3);

解决方案 2

var products = 
   _context.ProductType1.Select(p => new { ProductType = 1, p.ProductId, p.ProductName })
   .Concat(context.ProductType2.Select(p => new { ProductType = 2, p.ProductId, p.ProductName }))
   .Concat(context.ProductType3.Select(p => new { ProductType = 3, p.ProductId, p.ProductName }));

var stocks = from st in _context.StockDetails
    join p in products on new { st.ProductType, st.ProductId } equals new { p.ProductType, p.ProductId }
    select new StockList
    {
        ProductId = st.ProductId,
        ProductType = st.ProductType,
        StockValue = st.StockValue,
        InOut = st.InOut,
        ProductName  = p.ProductName 
    };

解决方案 3

var stocks = from st in _context.StockDetails
    join p1 in _context.ProductType1 on new { st.ProductType, st.ProductId } equals new { ProductType = 1, p1.ProductId } into j
    from p1 in j.DefaultIfEmpty()
    join p2 in _context.ProductType1 on new { st.ProductType, st.ProductId } equals new { ProductType = 2, p2.ProductId } into j
    from p2 in j.DefaultIfEmpty()
    join p3 in _context.ProductType3 on new { st.ProductType, st.ProductId } equals new { ProductType = 3, p3.ProductId } into j
    from p3 in j.DefaultIfEmpty()
    select new StockList
    {
        ProductId = st.ProductId,
        ProductType = st.ProductType,
        StockValue = st.StockValue,
        InOut = st.InOut,
        ProductName  = p1.ProductName ?? p2.ProductName ?? p3.ProductName
    };

【讨论】:

  • 非常感谢@SvyatoslavDanyliv。我会尝试所有这些 :-) 大约 30% 的列是特定的,其余的是常见的。将所有产品迁移到一个表中会更好,即使它变得越来越大(可能总共有 150 列)?因为我会不止一次遇到这个问题。你怎么看?
  • 如果有 150 列,这是个坏主意。也许您必须选择 EF Core 5 应该支持的 TPT(Table Per Type)。我不知道它的性能如何,但我认为不好;)。无论如何,例如,解决方案 3 可以在常用函数中编写一次,然后在其他查询中使用。
猜你喜欢
  • 2016-10-09
  • 2010-10-30
  • 1970-01-01
  • 2014-02-16
  • 1970-01-01
  • 2021-01-01
  • 1970-01-01
  • 2020-10-31
  • 2013-09-17
相关资源
最近更新 更多