LINQ语句中匿名类型的等效VB.NET语法是什么? [英] What's the equivalent VB.NET syntax for anonymous types in a LINQ statement?

查看:67
本文介绍了LINQ语句中匿名类型的等效VB.NET语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些C#LINQ代码转换为VB.NET,并停留在如何在VB.NET中声明匿名类型的问题上.

I'm trying to translate some C# LINQ code into VB.NET and am stuck on how to declare an anonymous type in VB.NET.

.Select(ci => 
    new { CartItem = ci, 
          Discount = DiscountItems.FirstOrDefault(di => di.SKU == ci.SKU) }) 

如何将C#的new { ... }语法转换为VB.NET?

How do you translate C#'s new { ... } syntax into VB.NET?

推荐答案

new { ... }变为

New With { ... }

New With {Key ... }如果要使用Key属性(允许您比较两个匿名类型实例,但不允许更改这些属性的值).

New With {Key ... } if you want to use Key properties (which allows you to compare two anonymous type instances but does not allow the values of those properties to be changed).

所以我猜你的陈述会像这样:

So I'm guessing your statement would look like:

.Select(Function(ci) New With {Key _
    .CartItem = ci, _
    .Discount = DiscountItems.FirstOrDefault(Function(di) di.SKU = ci.SKU) _
})

这篇关于LINQ语句中匿名类型的等效VB.NET语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆