C# 选择新的关键字组合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2263242/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:51:30  来源:igfitidea点击:

Select new keyword combination

c#linq

提问by GurdeepS

In LINQ, what does the select newkeyword combination do?

在LINQ中,select new关键字组合有什么作用?

I haven't found much documentation on this.

我还没有找到太多关于此的文档。

Thanks

谢谢

采纳答案by womp

By using select new, you can use the data or objects in the set you are working with to create new objects, either typed anonymouslyor normally.

通过使用select new,您可以使用您正在使用的集合中的数据或对象来创建新对象,无论是匿名输入还是正常输入。


1. Using select newto return new anonymously typed objects:


1.select new用于返回新的匿名类型对象:

var records = from person in people
              select new { person.name, person.id };

This uses the newkeyword to create a set of anonymously typed objects. A new object type is simply created by the compiler, with two properties in it. If you look at the object type in the debugger, you'll see that it has a crazy-looking, auto-generated type name. But you can also use the newkeyword just like you're used to outside of linq:

这使用new关键字来创建一组匿名类型的对象。编译器简单地创建了一个新的对象类型,其中包含两个属性。如果您查看调试器中的对象类型,您会发现它有一个看起来很疯狂的自动生成的类型名称。但是您也可以new像在 linq 之外使用的那样使用关键字:


2. Using select newto construct "regularly" typed objects:


2.select new用于构造“常规”类型对象:

var records = from person in people
              select new CreditRecord(person.creditLimit, person.name);

This example uses the newkeyword just like you're used to - to construct some known type of object via one of its constructors.

这个例子new就像你习惯的那样使用关键字 - 通过它的构造函数之一构造一些已知类型的对象。

Select is called a transformation (or projection) operator. It allows you to put the data in the set that you are working with through a transformation function, to give you a new object on the other side. In the examples above, we're simply "transforming" the person object into some other type by choosing only specific properties of the person object, and doing something with them. So the combination of select new ...is really just specifying the newoperation as the transformation function of the selectstatement. That might make more sense with a counter example to the above two:

Select 称为转换(或投影)运算符。它允许您通过转换功能将数据放入您正在使用的集合中,从而在另一侧为您提供一个新对象。在上面的例子中,我们只是通过只选择 person 对象的特定属性并用它们做一些事情来将 person 对象“转换”为其他类型。所以组合select new ...实际上只是将new操作指定为select语句的转换函数。对于上述两个反例,这可能更有意义:


3. Using selectwithout new, with no transformation


3. 使用selectwithout new,没有转换

Of course you do not need to use selectand newtogether. Take this example:

当然你不需要selectnew一起使用。拿这个例子:

var someJohns =  from person in people
                 where person.name == "John"
                 select person;

This gives you back the original object type that was in the set you were working with - no transformation, and no new object creation.

这使您返回您正在使用的集合中的原始对象类型 - 没有转换,也没有新的对象创建。


4. Using selectwithout new, with a transformation


4. 使用selectwithout new,带转换

And finally, a transformation without the newkeyword:

最后,没有new关键字的转换:

var personFoods = from person in people
                  select person.GetFavoriteFoods();

which gives you back some new type of object, generated from the transformation function and not by directly using the newkeyword to construct it.

它为您提供了一些新类型的对象,这些对象是从转换函数生成的,而不是直接使用new关键字来构造它。

回答by SLaks

It's the selectkeyword followed by a newexpression, probably creating an anonymous type.

它是select关键字后跟一个new表达式,可能会创建一个匿名类型

In other words, it's a combination of the following two unrelated expressions:

换句话说,它是以下两个不相关的表达式的组合:

select

select

from a in whatever select 2

Anonymous types

匿名类型

new { Prop1 = 3, Prop2 = DateTime.Now }

回答by Marcel Hymanwerth

It's not really a combination, newjust creates an anonymous type to return the value. Otherwise you would have to work with Dictionaries/Arrays etc (like in PHP) which is always ugly.

它不是真正的组合,new只是创建一个匿名类型来返回值。否则你将不得不使用字典/数组等(比如在 PHP 中),这总是很难看。

var q = 
    from staff in theWhiteHouseStaff
    select new { Name = staff.Name } ;