0

我可以使用一些帮助将此 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(predicate As System.Func(Of System.Web.UI.WebControls.GridViewRow, Integer, Boolean)) As System。 'System.Linq.Enumerable' 中定义的 Collections.Generic.IEnumerable(Of System.Web.UI.WebControls.GridViewRow)':嵌套函数与委托 'System.Func(Of System.Web.UI. WebControls.GridViewRow,整数,布尔值)'。扩展方法 'Public Function Where(predicate As System.Func(Of System.Web.UI.WebControls.GridViewRow, Boolean)) As System.Collections.Generic.IEnumerable(Of System.Web.UI.WebControls.GridViewRow)' 定义在“System.Linq.Enumerable”:未为“System.Web.UI”类型定义运算符“<>”。

还...

错误 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

4 回答 4

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()
于 2012-10-10T15:03:55.377 回答
1

在我的脑海中,我会说:

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

但我无法对此进行测试。

于 2012-10-10T15:04:31.700 回答
0
Dim rows = GridView1.Rows.Cast(Of GridViewRow)().Where(Function(a) a <> row).ToList()
于 2012-10-10T15:07:29.083 回答
0

根据这个转换器,它是

 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
于 2012-10-10T15:08:54.540 回答