将c#lambda表达式LINQ转换为vb.net

时间:2014-04-14 09:16:21

标签: c# vb.net linq lambda

我无法将以下查询从c#转换为vb。我不能正确的语法 LINQ可以,但不是很好。任何帮助表示赞赏。

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

1 个答案:

答案 0 :(得分:2)

我的方法是从有效的C#开始,并使用工具进行翻译。然后,根据生成的结果,我会根据需要进行一些调整。

这是一个与您类似的有效C#代码段,只有很小的改动。

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

将C#转换为VB.net的网站示例   - http://www.developerfusion.com/tools/convert/csharp-to-vb

这是生成的结果

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)