将VB.Net Linq转换为C#Linq

时间:2013-04-01 13:48:42

标签: c# vb.net linq

尝试将以下VB.net Linq转换为C#Linq

Dim query =
    From row In dt.AsEnumerable
    Group row By G = New With {.Price = row.Field(Of Double?)("price")}.Price,
                  New With {.Cat = row.Field(Of Integer?)("category")}.Cat
                 Into ProductGroups = Group

我尝试了一些网站但没有按预期工作。

2 个答案:

答案 0 :(得分:3)

在线转换器没有处理这个,所以这对你来说一定很令人沮丧。试试这个:

var query = from row in dt.AsEnumerable
            group row by new {Price = row.Field<double?>("price")}.Price, new {Cat = row.Field<int?>("category")}.Cat into g
            select new {G = g.Key, g};

答案 1 :(得分:2)

为了回答您的问题,而不是为您转换。确保在转换器中使用正确的语法。

以下是您要转换的内容:

Dim query = _
From row In dt.AsEnumerable _
Group row By G = New With {.Price = row.Field(Of Double?)("price")}.Price, _
              New With {.Cat = row.Field(Of Integer?)("category")}.Cat _
             Into ProductGroups = Group

即使在VB2012中不需要它,似乎在转换器中仍然使用_字符来指定该行上的语句不完整。

您现在可以使用link listed in the comments to the question进行转换。