C#中:有没有LINQ的方式来创建特定的构造函数的参数数组对象的数组? [英] C#: Is there a LINQ way to create an array of objects given an array of constructor parameters?

查看:228
本文介绍了C#中:有没有LINQ的方式来创建特定的构造函数的参数数组对象的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个例子,说我有名字的数组,我想通过调用构造函数字符串创建对象的数组名称

 类Person()
{
公共字符串名称{得到;组; }

公众人物(字符串名称)
{
名称=名称;
}
}

...

静态无效的主要()
{
的String [] =名称{彼得,禄,玛丽};
人[]的人;

/ *我能做到这一点,但我不知道是否有更好的方法。 * /
名单,LT;人>人=新的List<&人GT;();
的foreach(在名称字符串名称)
{
persons.Add(新的Person(名));
}

=人persons.ToArray();
}



我已经陷在NET 2.0世界太久现在我想在我目前的停机时间现代化...


解决方案

  //名字为string [] 
人[] =人names.Select(S = GT;新的Person(S))。ToArray的();



说明:



Enumerable.Select 是LINQ法投影。也就是说,服用个序列,并通过一些规则,它们投射到酒吧取值 FUNC<富,酒吧和GT; 吃掉和吐出酒吧秒。因此,

  names.Select(S = GT;新的Person(S))

是序列名称型的投影的IEnumerable<串> 来类型的序列的IEnumerable<&人GT; 。如果你知道函数式编程小青地图的作用。



现在,有一个微妙的点这里是值得理解;这几乎是肯定的LINQ中最重要但容易被误解的方面之一。这是延迟执行的概念。当我们说

 的IEnumerable<&人GT;人= names.Select(S = GT;新的Person(S)); 

这实际上并不进行投影(即,它尚未创建<$ C的情况下, $ C>人使用字符串 S IN 名称作为构造参数)构成。相反,它创建的东西,抓住了如何序列名称投影到序列中的规则。只有当该规则(称为一个迭代)被实际执行不突起发生



导致此执行来操作的一种方式是使用方法<一HREF =htt​​p://msdn.microsoft.com/en-us/library/bb298736.aspx> Enumerable.ToArray 这基本上是说通过迭代序列,并给我回的结果作为数组。



有其他的方式导致出现的执行。例如:



 的IEnumerable<&人GT;人= names.Select(S = GT;新的Person(S)); 
的foreach(在人者P){
Console.WriteLine(p.Name);
}

 的IEnumerable<&人GT;人= names.Select(S = GT;新的Person(S)); 
者P = persons.First();



这将执行第一投影(即新的Person(名[0] ))并分配结果 p



当然,这甚至没有进入什么

  S =>新的Person(S)



时。这是一个lambda表达式,你可以在我的答案得到一个介绍他们的如何做到这一点的LINQ表达工作?


As an example, say I have an array of names and I want to create an array of Person objects by calling a constructor that takes string name.

class Person()
{
    public string Name { get; set; }

    public Person(string name)
    {
        Name = name;
    }
}

...

static void Main()
{
    string[] names = {"Peter", "Paul", "Mary"};
    Person[] people;

    /*  I could do this but I'm wondering if there's a better way. */
    List<Person> persons = new List<Person>();
    foreach(string name in names)
    {
        persons.Add(new Person(name));
    }

    people = persons.ToArray();
}

I've been stuck in the .Net 2.0 world for too long now and I'm trying to modernize in my current downtime...

解决方案

// names is string[]
Person[] people = names.Select(s => new Person(s)).ToArray();

Explanation:

Enumerable.Select is the LINQ method for projection. That is, taking a sequence of Foos and projecting them to Bars via some rule Func<Foo, Bar> that eats Foos and spits out Bars. Thus

names.Select(s => new Person(s))

is a projection of the sequence names of type IEnumerable<string> to a sequence of type IEnumerable<Person>. If you know functional programming it plays the role of map.

Now, there is a subtle point here that is worth understanding; this is almost surely one of the most important yet easily misunderstood aspects of LINQ. This is the concept of deferred execution. When we say

IEnumerable<Person> persons = names.Select(s => new Person(s));

this does not actually perform the projection (i.e., it does not yet create the instances of Person constructed using the strings in names as constructor parameters). Instead, it creates something that captures the rule of how to project the sequence names into a sequence of Person. It's only when that rule (known as an iterator) is actually executed does the projection take place.

One way to cause this execution to occur is to use the method Enumerable.ToArray which basically says iterate through the sequence and give me back the results as an array.

There are other ways to cause the execution to occur. For example

IEnumerable<Person> persons = names.Select(s => new Person(s)); 
foreach(Person p in persons) {
    Console.WriteLine(p.Name);
}

or

IEnumerable<Person> persons = names.Select(s => new Person(s)); 
Person p = persons.First();

which would execute the "first" projection (i.e., new Person(names[0])) and assign the result to p.

Of course, this doesn't even get into exactly what

s => new Person(s)

is. That's a lambda expression, and you can get an introduction to them in my answer to How does this LINQ Expression work?.

这篇关于C#中:有没有LINQ的方式来创建特定的构造函数的参数数组对象的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆