Lambda 和 VB.NET

发布于 2024-09-24 23:25:39 字数 1220 浏览 2 评论 0原文

我在 StackOverflow 上找到了这个示例:

var people = new List<Person> {
    new Person{Name="aaa", Salary=15000, isHip=false}
    ,new Person{Name="aaa", Salary=15000, isHip=false}
    ,new Person{Name="bbb", Salary=20000, isHip=false}
    ,new Person{Name="ccc", Salary=25000, isHip=false}
    ,new Person{Name="ddd", Salary=30000, isHip=false}
    ,new Person{Name="eee", Salary=35000, isHip=false}
};


people.Where(p => p.Salary < 25000).Update(p => p.isHip = true);

foreach (var p in people)
{
Console.WriteLine("{0} - {1}", p.Name, p.isHip);
}

public static void Update<T>(this IEnumerable<T> source, Action<T> action)
{
foreach (var item in source)
    action(item);
}

在 C# 中一切正常。 我尝试在 VB.NET 中转换它。 这是代码:但是,

<System.Runtime.CompilerServices.Extension()> _
Public Sub Update(Of T)(ByVal source As IEnumerable(Of T), ByVal action As Action(Of T))
    For Each item In source
        action(item)
    Next item
End Sub

如果我尝试更新我的集合,事情就不起作用:

people.Where(Function(p) p.Salary < 25000).Update(Function(p) p.isHip = true)

我正在使用 VS2008 (3.5) 这件事让我发疯。

有人可以帮助我吗?

阿尔贝托

I have found this example on StackOverflow:

var people = new List<Person> {
    new Person{Name="aaa", Salary=15000, isHip=false}
    ,new Person{Name="aaa", Salary=15000, isHip=false}
    ,new Person{Name="bbb", Salary=20000, isHip=false}
    ,new Person{Name="ccc", Salary=25000, isHip=false}
    ,new Person{Name="ddd", Salary=30000, isHip=false}
    ,new Person{Name="eee", Salary=35000, isHip=false}
};


people.Where(p => p.Salary < 25000).Update(p => p.isHip = true);

foreach (var p in people)
{
Console.WriteLine("{0} - {1}", p.Name, p.isHip);
}

public static void Update<T>(this IEnumerable<T> source, Action<T> action)
{
foreach (var item in source)
    action(item);
}

In C# everything works fine.
I tried to convert it in VB.NET.
Here's the code:

<System.Runtime.CompilerServices.Extension()> _
Public Sub Update(Of T)(ByVal source As IEnumerable(Of T), ByVal action As Action(Of T))
    For Each item In source
        action(item)
    Next item
End Sub

If I try to update my collection things don't work, though:

people.Where(Function(p) p.Salary < 25000).Update(Function(p) p.isHip = true)

I am using VS2008 (3.5)
This thing is driving me crazy.

Is there anybody who can help me?

Alberto

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

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

发布评论

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

评论(1

夏日落 2024-10-01 23:25:39

您应该始终发布到底是什么不起作用


在您的情况下,您想要更新列表元素,这通过传递应该为每个元素运行的Action(Of T)来实现。

这样一个刚刚运行的操作会产生一些副作用,但不返回任何值,它由一个 VB 构造来描述:A Sub

因此,您想要编写的是

.Update(Sub(p) p.isHip = true)

有效的 VB2010,但在 2008 版本中不起作用。 C# 在那里没有问题,但在 VB 代码中,您想要传递一个 Function ,它必须生成一个值而不仅仅是执行赋值。 Func(Of ...) 将是该表达式的适当类型。

那么该怎么办?
您不能只用版本的语法来表达您想要的内容。但也许你不应该——在不修改旧集合的情况下构建一个新集合。一旦您处理值类型/属性,上述方法就根本不起作用,因为实际上 Where 返回的临时集合被修改了。 Linq 不是修改语言,而是一个查询系统。

无论如何:只需使用一个简单的循环即可。

You should always post what exactly is not working.


In your case, you want to Update list elements, which works though passing an Action(Of T) that should be run for every element.

Such an action, that is just run, performs some side-effects but returns no value is described by exactly one VB construct: A Sub.

Thus what you would want to write is

.Update(Sub(p) p.isHip = true)

which is valid VB2010, but simply does not work in the 2008 version. C# doesn't have a problem there, but in your VB code, you want to pass a Function which has to produce a value and not just perform an assignment. Func(Of ...) would be the appropriate type of that expression.

So what to do?
You can't just express what you want in the syntax of your version. But probably you shouldn't - build a new collection without modifying an old one. Is soon as you're dealing with value types/properties, the above approch won't work at all, since actually a temporary collection returned by Where is modified. Linq is no modification language, but a query system.

Anyway: Just use a plain loop.

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