vb.net 总和和组 linq

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14142256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:45:39  来源:igfitidea点击:

Sum and group linq

vb.netlinqgrouping

提问by nat

I have tried to sort this out myself but I am having to do it in VB which confuddles me even more. basically I have a lit of objects

我试图自己解决这个问题,但我不得不在 VB 中进行,这让我更加困惑。基本上我有一盏灯

List<Pax> paxes;

one of the Pax properties is Voucher, which in turn has some properties such as ID, and Price

Pax 属性之一是 Voucher,它又具有一些属性,例如 ID 和 Price

what I want to do is select out of a list of Pax, a list of Vouchers and their cumulative prices

我想要做的是从 Pax 列表、Vouchers 列表及其累计价格中选择

so from 
Pax1 - Voucher{ VoucherDesc = 10 percent off, VoucherCode = 10-OFF, Price = £150}
Pax2 - Voucher{ VoucherDesc = 10 percent off, VoucherCode = 10-OFF, Price = £120}
Pax3 - Voucher{ VoucherDesc = Buy one get one free, VoucherCode = BOGOF, Price = £300}

to 
{10 percent off, 10-OFF, £270 }
{Buy one get one free, BOGO, £300 }

I am sure this is possible with a bit of linq and grouping, but I am completely stumped

我确信这可以通过一些 linq 和分组来实现,但我完全被难住了

presume it is along the lines of

假设它是沿着

dim newlist = from p in paxes group p by p.VoucherCode into g _ 
                select new With {g. ..} 

yes this has to end up in VB but as I find that rather painful, figured I would make more sense of it in C# however, as the VB syntax is pure evil, if someone could post both, I might finally be able to make sense of what its doing. as the code converter doesn't seem to play nicely with extended linq queries.

是的,这必须在 VB 中结束,但因为我发现这相当痛苦,我想我会在 C# 中更有意义,但是,因为 VB 语法是纯粹的邪恶,如果有人可以同时发布两者,我可能最终能够理解它在做什么。因为代码转换器似乎不能很好地处理扩展的 linq 查询。

any help much appreciated

非常感谢任何帮助

采纳答案by Sergey Berezovskiy

Assume price is a decimal or double (not string):

假设价格是十进制或双精度(不是字符串):

var query = from v in paxes.Select(p => p.Voucher)
            group v by new {v.VoucherDesc, v.VoucherCode } into g
            select new { 
                Desc = g.Key.VoucherDesc,
                Code = g.Key.VoucherCode, 
                Total = g.Sum(x => x.Price)
            }; 

Update: you question was initially tagged with C#, thus this is a C# solution. Hope you can translate it to VB. If price is string, then just add parsing of value. E.g.

更新:您的问题最初是用 C# 标记的,因此这是一个 C# 解决方案。希望你能把它翻译成VB。如果价格是字符串,那么只需添加值的解析。例如

Total = g.Sum(x => Double.Parse(x.Price.Replace("£", ""))


回答by nat

thanks for the replies

感谢您的回复

managed to convert that to vb thus

设法将其转换为 vb 因此

Dim q = From v In paxes.Select(Function(p) p.Voucher).Where(Function(v) v IsNot Nothing) _
        Group v By v.OptionID, v.VoucherCode, v.Description Into Group _
        Select OptionID, VoucherCode, Description, TotalDiscount = Group.Sum(Function(v) v.Price)

fields are named a little different but it works fine helped by lazyb's post and this link: http://msdn.microsoft.com/en-us/vstudio/bb688088.aspx

字段的名称略有不同,但在lazyb 的帖子和此链接的帮助下它可以正常工作:http: //msdn.microsoft.com/en-us/vstudio/bb688088.aspx