我有一个CustomObject,用以下属性定义:
string Name {get;set;}
string EmailAddress{get;set;}
另外,我有一个单独的List<string>
,其中包含所有“名称”
我想在每个CustomObject中填充List<CustomObject>
,其名称来自List<string> names
。
我如何使用Linq实现这一目标?
答案 0 :(得分:4)
var strList = new List<string>();
var coList = strList.Select(item => new CustomObject() {Name = item}).ToList();
答案 1 :(得分:3)
尝试以下方法:
List<CustomObject> customObjectList =
(from item in stringList
select new CustomObject { Name = item, EmailAddress = String.Empty }).ToList();
答案 2 :(得分:2)
编辑:删除了垃圾类声明
IList<string> names = new List<string> { "first name", "second name" };
IList<SampleObject> myObjects =
names.Select(x => new SampleObject { Name = x }).ToList();