如何将sql联合转换为linq
我有以下使用联合的 Transact SQL 查询。 我需要一些关于 LINQ 中的外观的指示,即一些示例 如果有人能推荐一个关于 linq 中的 UNIONS 的好教程,那就太好了。
select top 10 Barcode, sum(ItemDiscountUnion.AmountTaken) from
(SELECT d.Barcode,SUM(AmountTaken) AmountTaken
FROM [Aggregation].[dbo].[DiscountPromotion] d
GROUP BY d.Barcode
UNION ALL
SELECT i.Barcode,SUM(AmountTaken) AmountTaken
FROM [Aggregation].[dbo].ItemSaleTransaction i
group by i.Barcode) ItemDiscountUnion
group by Barcode
请注意,原始 SQL 正在合并 2 个选择而不是连接它们。 我需要知道如何合并结果,即删除重复项并根据条形码对存在重复的行金额值求和。
I have the following Transact SQL query using a union.
I need some pointers as to how this would look in LINQ i.e some examples
wouldbe nice or if anyone can recommend a good tutorial on UNIONS in linq.
select top 10 Barcode, sum(ItemDiscountUnion.AmountTaken) from
(SELECT d.Barcode,SUM(AmountTaken) AmountTaken
FROM [Aggregation].[dbo].[DiscountPromotion] d
GROUP BY d.Barcode
UNION ALL
SELECT i.Barcode,SUM(AmountTaken) AmountTaken
FROM [Aggregation].[dbo].ItemSaleTransaction i
group by i.Barcode) ItemDiscountUnion
group by Barcode
Note the original SQL is merging the 2 selects NOT concatenating them.
I need to know how to merge the results i.e. removing duplicates and summing the rows amount value where there is duplication based on bar code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在集合上运行的三个有用的 Linq 概念。给定 set
c
和 sete
:Concat 为您提供
c
或e
中的所有内容:Union 还为您提供
c
和e
,但删除所有重复项:Ex except 为您提供
c
中e
中没有的所有内容:Three useful Linq concepts operating on sets. Given set
c
and sete
:Concat gives you everything in
c
ore
:Union also gives you everything in
c
ande
, but removes any duplicates:Except gives you everything in
c
that is not ine
:这是通用联合的示例,不考虑您发布的场景:
Here's an example of a generic union, without regard to the scenario you posted:
有 101 Linq 示例 - 包含两个联合示例Union1 和 Union2
此 Linq 语句应该会得到与 SQL 相同的结果:
(它对我来说有一个测试记录集)
There are the 101 Linq Samples - with two union samples Union1 and Union2
This Linq statement should get you the same results as your SQL:
(it has for me on a test record-set)