如何将嵌套的NOT EXISTS查询转换为LINQ

时间:2013-02-01 01:26:32

标签: linq tsql

知道如何将其转换为linq?

SELECT DISTINCT PartNumber AS [PartNumber That Includes EVERY ItemNumber Listed in @RequiredItemsTable]
  FROM Part.BillsOfMaterialsFlattened AS bom1 
  WHERE NOT EXISTS
       (SELECT *
          FROM @RequiredItemsTable w
         WHERE NOT EXISTS
               (SELECT *
                  FROM Part.BillsOfMaterialsFlattened AS bom2
                 WHERE (bom1.PartNumber = bom2.PartNumber)
                   AND (bom2.ItemNumber = w.ItemNumber)));

我在这个网站上看过几个例子,试图模仿它们......但到目前为止还没有到达那里。

谢谢!

这是我要通过linq实现的表格示例。上述T-SQL可以解决问题。

Part.BillsOfMaterialsFlattened
PartNumber  ItemNumber
10041047    10034480
10041047    10041090
10041047    10041091
10041048    10034480
10041048    10041091
10041049    10034650
10041049    10041090
10041049    10041091
10041050    10034650
10041050    10041091
10041061    10001331
10041061    10001422

@RequiredItemsTable
ItemNumber
10041090
10041091

希望LINQ语句生成IQueryable<String>

PartNumber (Part Numbers that include all Required Item Numbers)
10041047
10041049

1 个答案:

答案 0 :(得分:0)

以下linq可以解决问题:

var requiredItems = criteria.BomItemsIncluded.AsQueryable();
var partNumbers = (from b in _db.BillsOfMaterialsFlatteneds
                    group b by b.PartNumber into g
                    where requiredItems.All(r => g.Select(_ => _.ItemNumber).Contains(r))
                    select g.Key).Distinct();