C# Linq 将某些属性选择到另一个对象中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/923238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linq Select Certain Properties Into Another Object?
提问by Russ Bradberry
So say I have a collection of Bloops
所以说我有一个 Bloops 的集合
Class Bloop
Public FirstName
Public LastName
Public Address
Public Number
Public OtherStuff
End Class
Then I have a class of Razzies
然后我有一类Razzies
Class Razzie
Public FirstName
Public LastName
End Class
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?
是否可以使用 Linq 从 Bloops 集合中的所有 Bloops 中选择 FirstName 和 LastName 并返回 Razzies 集合?还是我仅限于使用 For-Loop 来完成我的工作?
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).
为了消除任何混淆,VB 或 C# 都可以。此外,这可能会导致我问(使用“Where”子句怎么样)的问题。
采纳答案by Noldorin
This should do the job:
这应该可以完成这项工作:
Dim results = From item In bloops _
Select New Razzie() With _
{ _
.FirstName = item.FirstName, _
.LastName = item.LastName _
}
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.
如果您想将结果从IEnumerable<Bloop>
(LINQ 查询返回的内容)转换为数组 or List<Bloop>
,只需分别附加对ToArray()
orToList()
扩展方法的调用。
Edit: Corrected the code so that it now has valid VB.NET 9 syntax.
编辑:更正了代码,使其现在具有有效的 VB.NET 9 语法。
回答by KClough
List<Bloop> myBloops = new List<Bloops>;
//populate myRazzies
List<Razzie> myRazzies = myBloops.Select(x => new Razzie() { FirstName = x.FirstName, LastName = x.LastName}).ToList();
回答by Amy B
Transforming from one type into another can be accomplished by using Enumerable.Select
可以使用Enumerable.Select完成从一种类型到另一种类型的转换
In fact, there is a samplefrom 101 linq samples that shows a query transforming ints into strings.
事实上,有一个样品从101个LINQ样品,显示了查询转化成整数字符串。
回答by nologo
public void Linq9()
{
string[] words = { "aPPLE", "BlUeBeRrY", "cHeRry" };
var upperLowerWords =
from w in words
select new { Upper = w.ToUpper(), Lower = w.ToLower() };
foreach (var ul in upperLowerWords)
{
Console.WriteLine("Uppercase: {0}, Lowercase: {1}", ul.Upper, ul.Lower);
}
}
回答by Md Saidul Karim
C# Sample - Thanks to earlier posters.
List<clsObj> myList = new List<clsObj>();
clsObj clsObjInstance = null;
for (int i = 0; i < 10; i++)
{
clsObjInstance = new clsObj() { x = (i+1) % 3, a = "A" + i.ToString() };
myList.Add(clsObjInstance);
}
List<int> extIntList = myList.Select(u => u.x).ToList();
foreach (int u in extIntList)
Console.Write(u.ToString() + "\t");
List<string> extStringList = myList.Select(u => u.a).ToList();
foreach (string u in extStringList)
Console.Write(u + "\t");