VB.NET 中的 lambda 表达式...我做错了什么?
当我运行这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能是因为 VB 使用它自己的比较运算符实现,而不是字符串类中的实现,因此 Load 方法无法使用该表达式。
尝试使用 Equals 方法:
如果这不起作用,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:
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.错误
当使用 LINQ VB.NET 代码通过比较操作查询 SharePoint 列表时,会发生这种情况。
根据 知识库 2883454:
The error
occurs when using LINQ VB.NET code that queries for SharePoint lists using a comparison operation.
According to KB 2883454: