是否可以在LINQ中创建相关子查询? [英] Is it possible to create a correlated subquery in LINQ?

查看:49
本文介绍了是否可以在LINQ中创建相关子查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个LINQ查询,该查询返回给定产品编号的父帐户及其所有子帐户的所有数量的总和.

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')


使用纯LINQ语法有可能吗?我需要改用扩展方法语法吗?

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

谢谢, -基思(Keith)

Thanks, -Keith

推荐答案

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

类似的东西吗?

您可以替换

  subitem.AccountNumber == 27748

   subitem.AccountNumber == item.AccountNumber

如果这就是您想要的

这篇关于是否可以在LINQ中创建相关子查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆