LINQ的选择特定的到另一个对象的属性?属性、对象、LINQ

2023-09-02 02:09:51 作者:莫道年少不深情

所以说我有Bloops的集合

So say I have a collection of Bloops

Class Bloop
  Public FirstName
  Public LastName
  Public Address
  Public Number
  Public OtherStuff
End Class

然后,我有一类金酸梅的

Then I have a class of Razzies

Class Razzie
  Public FirstName
  Public LastName
End Class

使用LINQ to Bloops集合中选择名字和姓氏了所有的Bloops并返回金酸梅的集合,这可能吗?还是我仅限于一个for循环做我的工作?

Is it possible using Linq to select the FirstName and LastName out of all the Bloops in the collection of Bloops and return a collection of Razzies? Or am i limited to a For-Loop to do my work?

要消除混淆,无论是VB或C#都行。这也可能会导致我问的问题(怎么样使用去哪儿的条款)。

To clear up any confusion, either VB or C# will do. Also this will probably lead to me asking the question of (What about using a "Where" clause).

推荐答案

这应该做的工作:

Dim results = From item In bloops _
              Select New Razzie() With _
              { _
                  .FirstName = item.FirstName, _
                  .LastName = item.LastName _
              }

如果你想从的结果转换的​​IEnumerable< BLOOP> (什么LINQ查询返回)到一个数组或名单,其中,BLOOP> ,只是分别追加调用的ToArray()了ToList()扩展方法

And if you want to convert the result from IEnumerable<Bloop> (what the LINQ query returns) to an array or List<Bloop>, just append a call to the ToArray() or ToList() extension methods respectively.

编辑:更正了code,以便它现在具有有效的VB.NET 9语法

Corrected the code so that it now has valid VB.NET 9 syntax.