CodeConverter
CodeConverter copied to clipboard
VB -> C#: Error converting LINQ query with Aggregate
VB.Net input code
Public Class VisualBasicClass
Public Class C1
Public Property C2p As C2
Public Property A As Decimal
End Class
Public Class C2
Public Property P1 As String
Public Property P2 As String
Public Property P3 As String
End Class
Sub Main
Dim lst = New List(Of C1) From {
{New C1() With {.C2p = New C2 With {.P1 = "1", .P2 = "1", .P3 = "1"}, .A = 100}},
{New C1() With {.C2p = New C2 With {.P1 = "1", .P2 = "1", .P3 = "1"}, .A = 100}},
{New C1() With {.C2p = New C2 With {.P1 = "2", .P2 = "2", .P3 = "2"}, .A = 100}}
}
Dim tSummary = From t In lst
Group By t.C2p.P1, t.C2p.P2, t.C2p.P3 Into Summary = Sum(t.A)
Where Summary > 0
Select P1, P2, P3, Summary
End Sub
End Class
Erroneous output
using System.Collections.Generic;
using System.Linq;
public partial class VisualBasicClass
{
public partial class C1
{
public C2 C2p { get; set; }
public decimal A { get; set; }
}
public partial class C2
{
public string P1 { get; set; }
public string P2 { get; set; }
public string P3 { get; set; }
}
public void Main()
{
var lst = new List<C1>() { { new C1() { C2p = new C2() { P1 = "1", P2 = "1", P3 = "1" }, A = 100m } }, { new C1() { C2p = new C2() { P1 = "1", P2 = "1", P3 = "1" }, A = 100m } }, { new C1() { C2p = new C2() { P1 = "2", P2 = "2", P3 = "2" }, A = 100m } } };
var tSummary = from t in lst
group t by new { t.C2p.P1, t.C2p.P2, t.C2p.P3 } into Group
select new { P1, P2, P3, Summary };
}
}
Expected output
using System.Collections.Generic;
using System.Linq;
public partial class VisualBasicClass
{
public partial class C1
{
public C2 C2p { get; set; }
public decimal A { get; set; }
}
public partial class C2
{
public string P1 { get; set; }
public string P2 { get; set; }
public string P3 { get; set; }
}
public void Main()
{
var lst = new List<C1>() { { new C1() { C2p = new C2() { P1 = "1", P2 = "1", P3 = "1" }, A = 100m } }, { new C1() { C2p = new C2() { P1 = "1", P2 = "1", P3 = "1" }, A = 100m } }, { new C1() { C2p = new C2() { P1 = "2", P2 = "2", P3 = "2" }, A = 100m } } };
var tSummary = from t in lst
group t.A by new { t.C2p.P1, t.C2p.P2, t.C2p.P3 } into Group
let Summary = Group.Sum()
where Summary > 0
select new { Group.Key.P1, Group.Key.P2, Group.Key.P3, Summary }
;
}
}
Details
- Product in use: website codeconverter.icsharpcode.net
- Version in use: 8.4.7.0
- Never saw it working
- I tried several version of the VB syntax, and couldn't get a variation that would convert
Thanks for the report. Just the kind of example case needed
Having the key properties in C2 isn’t necessary for the example, it just made it closer to my original starting point, and I didn’t want to stray to far from that until I understood and had everything pinned down.