C# 使用 lambda 表达式将对象列表从一种类型转换为另一种类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1909268/
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
convert a list of objects from one type to another using lambda expression
提问by Stratton
I have a foreach loop reading a list of objects of one type and producing a list of objects of a different type. I was told that a lambda expression can achieve the same result.
我有一个 foreach 循环读取一种类型的对象列表并生成不同类型的对象列表。有人告诉我 lambda 表达式可以达到相同的结果。
var origList = List<OrigType>(); // assume populated
var targetList = List<TargetType>();
foreach(OrigType a in origList) {
targetList.Add(new TargetType() {SomeValue = a.SomeValue});
}
Any help would be appreciated- i'm new to lambda and linq thanks, s
任何帮助将不胜感激 - 我是 lambda 和 linq 的新手,谢谢
回答by JaredPar
Try the following
尝试以下
var targetList = origList
.Select(x => new TargetType() { SomeValue = x.SomeValue })
.ToList();
This is using a combination of Lambdas and LINQ to achieve the solution. The Select function is a projection style method which will apply the passed in delegate (or lambda in this case) to every value in the original collection. The result will be returned in a new IEnumerable<TargetType>
. The .ToList call is an extension method which will convert this IEnumerable<TargetType>
into a List<TargetType>
.
这是使用 Lambdas 和 LINQ 的组合来实现解决方案。Select 函数是一种投影样式方法,它将传入的委托(在本例中为 lambda)应用于原始集合中的每个值。结果将以新的IEnumerable<TargetType>
. .ToList 调用是一个扩展方法,它将把它转换IEnumerable<TargetType>
成List<TargetType>
.
回答by Andy White
I believe something like this should work:
我相信这样的事情应该有效:
origList.Select(a => new TargetType() { SomeValue = a.SomeValue});
回答by Jon Skeet
If you knowyou want to convert from List<T1>
to List<T2>
then List<T>.ConvertAll
will be slightly more efficient than Select
/ToList
because it knows the exact size to start with:
如果您知道要从 to 转换List<T1>
,List<T2>
那么List<T>.ConvertAll
将比Select
/稍微高效一点,ToList
因为它知道开始时的确切大小:
target = orig.ConvertAll(x => new TargetType { SomeValue = x.SomeValue });
In the more general case when you only know about the source as an IEnumerable<T>
, using Select
/ToList
is the way to go. You couldalso argue that in a world with LINQ, it's more idiomatic to start with... but it's worth at least being aware of the ConvertAll
option.
在更一般的情况下,当您只知道作为 的源时IEnumerable<T>
,使用Select
/ToList
是要走的路。您也可以争辩说,在使用 LINQ 的世界中,开始时更惯用……但至少值得了解该ConvertAll
选项。
回答by Chris Arnold
var list1 = new List<Type1>();
var list2 = new List<Type2>();
list1.ForEach(item => list2.Add(new Type2() { Prop1 = value1 }));
回答by Ian P
Here's a simple example..
这是一个简单的例子..
List<char> c = new List<char>() { 'A', 'B', 'C' };
List<string> s = c.Select(x => x.ToString()).ToList();
回答by Pranav
List<target> targetList = new List<target>(originalList.Cast<target>());
回答by Alp
var target = origList.ConvertAll(x => (TargetType)x);
回答by jaimenino
If you need to use a function to cast:
如果您需要使用函数进行转换:
var list1 = new List<Type1>();
var list2 = new List<Type2>();
list2 = list1.ConvertAll(x => myConvertFuntion(x));
Where my custom function is:
我的自定义函数在哪里:
private Type2 myConvertFunction(Type1 obj){
//do something to cast Type1 into Type2
return new Type2();
}
回答by Max Chu
Assume that you have multiple properties you want to convert.
假设您有多个要转换的属性。
public class OrigType{
public string Prop1A {get;set;}
public string Prop1B {get;set;}
}
public class TargetType{
public string Prop2A {get;set;}
public string Prop2B {get;set;}
}
var list1 = new List<OrigType>();
var list2 = new List<TargetType>();
list1.ConvertAll(x => new OrigType { Prop2A = x.Prop1A, Prop2B = x.Prop1B })
回答by Arun Solanki
for similar type class.
对于类似类型的类。
List<targetlist> targetlst= JsonConvert.DeserializeObject<List<targetlist>>(JsonConvert.SerializeObject(<List<baselist>));
List<targetlist> targetlst= JsonConvert.DeserializeObject<List<targetlist>>(JsonConvert.SerializeObject(<List<baselist>));