如果使用 EF Linq 进行 INNER JOIN 时有 Or 条件该怎么办?

问题描述 投票:0回答:1

下面的代码我有一个 TSql 查询,其连接中有 Or 条件。如果我想将其转换为 C# 中的 Linq,我该如何重写它。

    SELECT P.Id AS ProductId
      FROM generalinv.InventoryTypeProductTypeMapping AS ITM
INNER JOIN master.Product AS P 
        ON (P.ProductCategoryId = ISNULL(ITM.ProductCategoryId, 0))
        OR (P.ProductTypeId = ISNULL(ITM.ProductTypeId, 0))
c# sql linq
1个回答
0
投票

您可以使用 from 子句,然后使用应用 OR 条件的 where 子句过滤结果,也代替了我使用的 ISNULL ??:

from itm in generalinv.InventoryTypeProductTypeMapping
from p in master.Product
where p.ProductCategoryId == (itm.ProductCategoryId ?? 0) 
      || p.ProductTypeId == (itm.ProductTypeId ?? 0)
                
© www.soinside.com 2019 - 2024. All rights reserved.