Is it possible to create a correlated subquery in LINQ?

做~自己de王妃 提交于 2019-12-21 20:18:13

问题


I'd like to create a LINQ query that returns the sum of all quantities for a given productnumber for a parent account and all it's child accounts.

I have a table of products by account number in which each row also contains a qty and the parent account number:


PartNumber   AccountNumber   ParentAccountNumber   Qty
----------   -------------   -------------------   ---
1000000390   27113           27173                  2
1000000390   27516           27173                  1
1000000390   00113           27173                  0
1000000390   27748           27173                  5


SELECT * FROM Inventory
WHERE ProductNumber='1000000390' 
AND ParentAccountNumber=(SELECT TOP 1 parentaccountnumber FROM Inventory 
WHERE accountnumber='27748')


is this possible in pure LINQ syntax? Do I need to use extension method syntax instead?

Thanks, -Keith


回答1:


from item in Inventory
where item.ProductNumber == 1000000390
where item.ParentAccountNumber == (from subitem in Inventory 
                                   where subitem.AccountNumber == 27748
                                   select subitem.ParentAccountNumber).First()
select item

Something like that?

You can replace

  subitem.AccountNumber == 27748

with

   subitem.AccountNumber == item.AccountNumber

if that's what you wanted



来源:https://stackoverflow.com/questions/2225052/is-it-possible-to-create-a-correlated-subquery-in-linq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!