VB.NET 中的 Lambda 表达式... 我做错了什么?

3
当我运行这段C#代码时,没有问题...但是当我将其翻译成VB.NET时,它可以编译但由于“CompareString”成员在表达式中不被允许而崩溃...我感觉我在这里漏掉了一些关键的东西...
private void PrintButton_Click(object sender, EventArgs e)
{
if (ListsListBox.SelectedIndex > -1)
{
    //Context
    using (ClientOM.ClientContext ctx =
        new ClientOM.ClientContext(UrlTextBox.Text))
    {
        //Get selected list
        string listTitle = ListsListBox.SelectedItem.ToString();
        ClientOM.Web site = ctx.Web;
        ctx.Load(site,
            s => s.Lists.Where(l => l.Title == listTitle));
        ctx.ExecuteQuery();

        ClientOM.List list = site.Lists[0];

        //Get fields for this list
        ctx.Load(list,
            l => l.Fields.Where(f => f.Hidden == false 
      && (f.CanBeDeleted == true || f.InternalName == "Title")));
        ctx.ExecuteQuery();

        //Get items for the list
     ClientOM.ListItemCollection listItems = list.GetItems(
      ClientOM.CamlQuery.CreateAllItemsQuery());
        ctx.Load(listItems);
        ctx.ExecuteQuery();

        // DOCUMENT CREATION CODE GOES HERE

    }

    MessageBox.Show("Document Created!");
}

但是在VB.NET代码中,由于ctx.Load()方法不允许使用“CompareString”成员,因此会出现错误...

Private Sub PrintButton_Click(sender As Object, e As EventArgs)
    If ListsListBox.SelectedIndex > -1 Then
        'Context
        Using ctx As New ClientOM.ClientContext(UrlTextBox.Text)
            'Get selected list
            Dim listTitle As String = ListsListBox.SelectedItem.ToString()
            Dim site As ClientOM.Web = ctx.Web
            ctx.Load(site, Function(s) s.Lists.Where(Function(l) l.Title = listTitle))
            ctx.ExecuteQuery()

            Dim list As ClientOM.List = site.Lists(0)

            'Get fields for this list
            ctx.Load(list, Function(l) l.Fields.Where(Function(f) f.Hidden = False AndAlso (f.CanBeDeleted = True OrElse f.InternalName = "Title")))
            ctx.ExecuteQuery()

            'Get items for the list
            Dim listItems As ClientOM.ListItemCollection = list.GetItems(ClientOM.CamlQuery.CreateAllItemsQuery())
            ctx.Load(listItems)

            ' DOCUMENT CREATION CODE GOES HERE

            ctx.ExecuteQuery()
        End Using

        MessageBox.Show("Document Created!")
    End If
End Sub
2个回答

2

这可能是因为VB使用了自己实现的比较运算符,而不是字符串类中的实现方式,所以该表达式不能被Load方法使用。

尝试使用Equals方法:

ctx.Load(site, Function(s) s.Lists.Where(Function(l) l.Title.Equals(listTitle)))

如果那不起作用,LINQ中有一个eq运算符可以在表达式中使用,但我必须查找VB的语法。

实际上,所有关于此的字符串操作似乎都不受支持,这很奇怪,因为比较中的两个项目都是字符串... - Bob

1

当使用比较操作查询SharePoint列表的LINQ VB.NET代码时,会出现错误。

未处理的异常: Microsoft.SharePoint.Client.ClientRequestException: 表达式中不能使用“CompareString”成员。

根据KB 2883454

The cause for this issue is that in VB.NET, the expression:

s = "abc"

is converted to an expression:

0 == Microsoft.VisualBasic.CompilerServices.Operators.CompareString(s, "abc", true)

which is not supported by SharePoint code.

There is no fix for this currently, but a workaround would be to compile the above logic in a C#.NET module and use it in your VB.NET code.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接