Linq 将某些属性选择到另一个对象中? [英] Linq Select Certain Properties Into Another Object?
问题描述
所以说我有一组 Bloops
So say I have a collection of Bloops
Class Bloop
Public FirstName
Public LastName
Public Address
Public Number
Public OtherStuff
End Class
然后我有一类 Razzies
Then I have a class of Razzies
Class Razzie
Public FirstName
Public LastName
End Class
是否可以使用 Linq 从 Bloops 集合中的所有 Bloops 中选择 FirstName 和 LastName 并返回 Razzies 集合?还是我只能使用 For-Loop 来完成我的工作?
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# 都可以.此外,这可能会导致我问(使用Where"子句怎么样)的问题.
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
(LINQ 查询返回的内容)转换为数组或 List
,只需附加一个调用分别到 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.
更正了代码,使其现在具有有效的 VB.NET 9 语法.
Corrected the code so that it now has valid VB.NET 9 syntax.
这篇关于Linq 将某些属性选择到另一个对象中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!