0

I am having trouble converting the following query to vb from c#. I cant get the syntax right and am ok but not great with LINQ. Any help appreciated.

var result = (From d In projectionEntities.projections
             Where d.SymbolId <= 42
             Join t In projectionEntities.symbols On d.SymbolId Equals t.Id                
             Group d by d.SymbolId into g
             select new {
                     SymbolId = g.Key,
                       ProjectionPerformances = 
                                   g.Select(gg=>new ProjectionPerformance{
                                               SymbolId = gg.SymbolId,
                                               Name = gg.Symbol.Name,
                                               rpDate = gg.Date.ToString(),
                                               ActualRange = gg.HighProjection - gg.LowProjection
                                              })
                  .ToDictionary(g=>g.SymbolId);
2
  • Please post your VB.Net Linq code as well. Maybe there's just a little glitch in it Commented Apr 14, 2014 at 10:25
  • 1
    In the future please don't mix languages in the same code snippet (your code is an odd mixture of C# and VB) - it just adds more work to figuring out the problem. Commented Apr 14, 2014 at 14:33

1 Answer 1

2

My approach is to start from valid C# and use a tool to do the translation. Then from the generated result I make few adjustments if needed.

This is a valid C# snippet similar to yours, with minor alterations.

var projections = new[]
{
    new { SymbolId = 1, Name = "", Date = DateTime.Now }, 
    new { SymbolId = 2, Name = "", Date = DateTime.Now }
};
var symbols = new[] { new { Id = 1 }, new { Id = 2 } };

var result = 
(from p in projections
            where p.SymbolId <= 42
            join s in symbols on p.SymbolId equals s.Id
            group p by p.SymbolId into g
            select new
            {
                SymbolId = g.Key,
                ProjectionPerformances =
                            g.Select(gg => new
                            {
                                SymbolId = gg.SymbolId,
                                Name = gg.Name,
                                rpDate = gg.Date.ToString(),
                            }
                                        )
            }).ToDictionary(g => g.SymbolId);

Example of a website which translates C# to VB.net - http://www.developerfusion.com/tools/convert/csharp-to-vb

This is the generated result

Dim projections = New () {New With { _
    Key .SymbolId = 1, _
    Key .Name = "", _
    Key .[Date] = DateTime.Now _
}, New With { _
    Key .SymbolId = 2, _
    Key .Name = "", _
    Key .[Date] = DateTime.Now _
}}
Dim symbols = New () {New With { _
    Key .Id = 1 _
}, New With { _
    Key .Id = 2 _
}}

Dim result = (From g In From p In projections 
Where p.SymbolId <= 42Join s 
In symbols On p.SymbolId = s.IdGroup p By p.SymbolIdNew With { _
    Key .SymbolId = g.Key, _
    Key .ProjectionPerformances = g.[Select](Function(gg) New With { _
        Key .SymbolId = gg.SymbolId, _
        Key .Name = gg.Name, _
        Key .rpDate = gg.[Date].ToString() _
    }) _
}).ToDictionary(Function(g) g.SymbolId)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.