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

发布于 2024-08-24 16:40:15 字数 2357 浏览 5 评论 0原文

当我运行这个 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

when I run this C# code, no problems... but when I translate it into VB.NET it compiles but blows due to 'CompareString' member not being allowed in the expression... I feel like I'm missing something key here...

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!");
}

}

but in VB.NET code this errors due to not being allowed 'CompareString' members in the ctx.Load() methods...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

箜明 2024-08-31 16:40:15

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

尝试使用 Equals 方法:

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

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

That is probably because VB is using it's own implementation of the comparison operator, not the implementation in the string class, so the expression can't be used by the Load method.

Try using the Equals method:

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

If that doesn't work, there is an eq operator in LINQ that works in expressions, but I would have to look up the VB syntax for that.

夜巴黎 2024-08-31 16:40:15

错误

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

当使用 LINQ VB.NET 代码通过比较操作查询 SharePoint 列表时,会发生这种情况。

根据 知识库 2883454

导致此问题的原因是在 VB.NET 中,表达式:

<前><代码>s = "abc"

转换为表达式:

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

SharePoint 代码支持该功能。

目前尚无解决此问题的方法,但解决方法是
在 C#.NET 模块中编译上述逻辑并在 VB.NET 中使用它
代码。

The error

Unhandled Exception:
Microsoft.SharePoint.Client.ClientRequestException: The
'CompareString' member cannot be used in the expression.

occurs when using LINQ VB.NET code that queries for SharePoint lists using a comparison operation.

According to 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文