使用Lambda和Func <>的动态构造函数

Hoo*_*ake 1 c# linq design-patterns func

我有一个多线程应用程序,它在BlockingCollection队列上创建一个字符串列表,我想获取该字符串列表并将其转换为一个或两个步骤中的项目对象集合

是否可以创建一个func <>或lamda方法来实现这种类型的结果

 public class item
{
    public string name { get; set; }

    public item(string nam)
    {
        name = nam;
    }


}

IList<string> alist = new string[] {  "bob","mary"};
Run Code Online (Sandbox Code Playgroud)

你在哪里获取类型字符串的Ilist <>或IEnumerable <>并返回IList

所以对于单项Func <>

Func<string, item> func1 = x => new item(x);
Run Code Online (Sandbox Code Playgroud)

但标签看起来很像

Func<IEnumerable<string>,IList<item>> func2 = x=> x.ForEach(i => func1(i));
Run Code Online (Sandbox Code Playgroud)

我试图在sqaure孔中放置一个圆形挂钩,或者我的语法/逻辑是错误的

提前致谢

Dav*_*tka 5

你只是试图"重塑"的IList<string>作为IList<item>

IList<string> listOfStrings = new string[] {  "bob","mary"};
IList<item> listOfItems = listOfStrings.Select(s => new item(s)).ToList();
Run Code Online (Sandbox Code Playgroud)