下面的代码我有一个 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))
您可以使用 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)