将C#Linq翻译为VB

时间:2012-10-10 15:02:07

标签: c# vb.net linq

我可以使用一些帮助将这个C#代码行转换为VB。我对Linq很新,我的语法有问题......

var rows = GridView1.Rows.Cast<GridViewRow>().Where(a => a != row).ToList();

我尝试了很多在线转换工具,但也没有一个能够正确使用它。

更新: 这是完整的代码块......

protected void MoveGridViewRows(object sender, EventArgs e)  {      
    Button btnUp = (Button)sender;      
    GridViewRow row = (GridViewRow)btnUp.NamingContainer;      
    // Get all items except the one selected      
    var rows = GridView1.Rows.Cast<GridViewRow>().Where(a => a != row).ToList();      
    switch (btnUp.CommandName)      
    {          
         case "Up":              
         //If First Item, insert at end (rotating positions)              
              if (row.RowIndex.Equals(0))
                  rows.Add(row);              
              else                 
                  rows.Insert(row.RowIndex - 1, row);              
                  break;          
         case "Down":              
         //If Last Item, insert at beginning (rotating positions)              
              if (row.RowIndex.Equals(GridView1.Rows.Count - 1))                  
                  rows.Insert(0, row);              
              else                 
                  rows.Insert(row.RowIndex + 1, row);              
                  break;      
         }      
         GridView1.DataSource = rows.Select(a => new     
         {          
              FirstName = ((TextBox)a.FindControl("txtFirstName")).Text,          
              LastName = ((TextBox)a.FindControl("txtLastName")).Text,      
         }).ToList();      
         GridView1.DataBind();  
} 

编译时VS中的特定错误是....

错误11重载解析失败,因为无法使用这些参数调用可访问的“Where”:     扩展方法'Public Function Where(谓词As System.Func(Of System.Web.UI.WebControls.GridViewRow,Integer,Boolean))As System.Collections.Generic.IEnumerable(Of System.Web.UI.WebControls.GridViewRow)'在'System.Linq.Enumerable'中定义:嵌套函数与委托'System.Func(Of System.Web.UI.WebControls.GridViewRow,Integer,Boolean)'的签名不同。     扩展方法'Public Function Where(谓词As System.Func(Of System.Web.UI.WebControls.GridViewRow,Boolean))As System.Collections.Generic.IEnumerable(Of System.Web.UI.WebControls.GridViewRow)'in defined in 'System.Linq.Enumerable':运算符'&lt;&gt;'没有为类型'System.Web.UI.WebControls.GridViewRow'和'System.Web.UI.WebControls.GridViewRow'定义。

也...

错误12未声明名称“a”。 C:\ Users \ Documents \ Visual Studio 2008 \ WebSites \ vv_home \ roeMgr.aspx.vb 51 46 C:\

这是迄今为止的VB代码.....

    Protected Sub MoveGridViewRows(ByVal sender As Object, ByVal e As EventArgs)


    Dim btnUp As Button = DirectCast(sender, Button)
    Dim row As GridViewRow = DirectCast(btnUp.NamingContainer, GridViewRow)
    ' Get all items except the one selected      
    Dim rows = GridView1.Rows.Cast(Of GridViewRow)().Where(Function(a) a IsNot row).ToList()


    Select Case btnUp.CommandName
        Case "Up"
            'If First Item, insert at end (rotating positions)              
            If row.RowIndex.Equals(0) Then
                rows.Add(row)
            Else
                rows.Insert(row.RowIndex - 1, row)
            End If
            Exit Select
        Case "Down"
            'If Last Item, insert at beginning (rotating positions)              
            If row.RowIndex.Equals(GridView1.Rows.Count - 1) Then
                rows.Insert(0, row)
            Else
                rows.Insert(row.RowIndex + 1, row)
            End If
            Exit Select
    End Select
    GridView1.DataSource = rows.[Select](a >= New With { _
     .FirstName = DirectCast(a.FindControl("txtFirstName"), TextBox).Text, _
     .LastName = DirectCast(a.FindControl("txtLastName"), TextBox).Text _
    }).ToList()
    GridView1.DataBind()


End Sub

感谢,

4 个答案:

答案 0 :(得分:2)

Dim rows = GridView1.Rows.Cast(Of GridViewRow)().Where(Function(a) a IsNot row).ToList()

为另一个,应该是

GridView1.DataSource = rows.Select(Function(a) New With {_         
              .FirstName = CType(a.FindControl("txtFirstName"), TextBox).Text,_
              .LastName = CType(a.FindControl("txtLastName"), TextBox).Text_      
         }).ToList()

答案 1 :(得分:1)

我不敢说:

dim rows = GridView1.Rows.Cast(of GridViewRow)().Where(Function(a) a <> row).ToList()

但是我无法测试它。

答案 2 :(得分:0)

Dim rows = GridView1.Rows.Cast(Of GridViewRow)().Where(Function(a) a <> row).ToList()

答案 3 :(得分:0)

根据this converter,它是

 Dim rows = GridView1.Rows.Cast(Of GridViewRow)().Where(Function(a) a <> row).ToList()

这对我来说是正确的。


由于您发布了完整的代码块,因此转换整个代码的结果如下:

Protected Sub MoveGridViewRows(sender As Object, e As EventArgs)
    Dim btnUp As Button = DirectCast(sender, Button)
    Dim row As GridViewRow = DirectCast(btnUp.NamingContainer, GridViewRow)
    ' Get all items except the one selected      
    Dim rows = GridView1.Rows.Cast(Of GridViewRow)().Where(Function(a) a <> row).ToList()
    Select Case btnUp.CommandName
        Case "Up"
            'If First Item, insert at end (rotating positions)              
            If row.RowIndex.Equals(0) Then
                rows.Add(row)
            Else
                rows.Insert(row.RowIndex - 1, row)
            End If
            Exit Select
        Case "Down"
            'If Last Item, insert at beginning (rotating positions)
            If row.RowIndex.Equals(GridView1.Rows.Count - 1) Then
                rows.Insert(0, row)
            Else
                rows.Insert(row.RowIndex + 1, row)
            End If
            Exit Select
    End Select
    GridView1.DataSource = rows.[Select](Function(a) New With { _
        Key .FirstName = DirectCast(a.FindControl("txtFirstName"), TextBox).Text, _
        Key .LastName = DirectCast(a.FindControl("txtLastName"), TextBox).Text _
    }).ToList()
    GridView1.DataBind()
End Sub