I could use some help converting this C# line of code to VB. I am very new to Linq and am having trouble with the syntax…
var rows = GridView1.Rows.Cast<GridViewRow>().Where(a => a != row).ToList();
I have tried a number of online conversion tools and none of them have gotten it right either.
UPDATE:
Here is the full code block….
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();
}
The specific error in VS on compile is….
Error 11 Overload resolution failed because no accessible ‘Where’ can be called with these arguments:
Extension method ‘Public Function Where(predicate As System.Func(Of System.Web.UI.WebControls.GridViewRow, Integer, Boolean)) As System.Collections.Generic.IEnumerable(Of System.Web.UI.WebControls.GridViewRow)’ defined in ‘System.Linq.Enumerable’: Nested function does not have the same signature as delegate ‘System.Func(Of System.Web.UI.WebControls.GridViewRow, Integer, Boolean)’.
Extension method ‘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)’ defined in ‘System.Linq.Enumerable’: Operator ‘<>’ is not defined for types ‘System.Web.UI.WebControls.GridViewRow’ and ‘System.Web.UI.WebControls.GridViewRow’.
Also…
Error 12 Name ‘a’ is not declared. C:\Users\Documents\Visual Studio 2008\WebSites\vv_home\roeMgr.aspx.vb 51 46 C:\
HERE is the VB code thus far…..
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
thanks,
for the other, should be