选择新的关键字组合 [英] Select new keyword combination

查看:79
本文介绍了选择新的关键字组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In LINQ, what does the select new keyword combination do?

我没有找到太多有关此的文档.

I haven't found much documentation on this.

谢谢

推荐答案

通过使用select new,您可以使用正在处理的数据集中的数据或对象来创建新对象,方法是键入

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


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


1. Using select new to return new anonymously typed objects:

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

这使用new关键字来创建一组匿名键入的对象.新的对象类型由编译器简单地创建,其中具有两个属性.如果在调试器中查看对象类型,您会发现它具有一个看上去很疯狂的,自动生成的类型名称.但是您也可以使用new关键字,就像您在linq之外已经习惯了:

This uses the new keyword 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 new keyword just like you're used to outside of linq:


2.使用select new构造常规"类型的对象:


2. Using select new to construct "regularly" typed objects:

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

此示例使用new关键字,就像您习惯使用-通过其构造函数之一构造某种已知类型的对象一样.

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

Select被称为变换(或投影)运算符.它允许您通过转换函数将数据放入正在使用的集合中,从而在另一端为您提供一个新对象.在上面的示例中,我们仅通过选择人员对象的特定属性并对它们进行处理,就可以简单地将人员对象转换"为其他类型.因此,select new ...的组合实际上只是将new操作指定为select语句的转换函数.与上面两个相反的示例可能更有意义:

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 new operation as the transformation function of the select statement. That might make more sense with a counter example to the above two:


3.在不使用new的情况下使用select,不进行转换


3. Using select without new, with no transformation

当然,您无需同时使用selectnew.举个例子:

Of course you do not need to use select and new together. Take this example:

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.在没有new的情况下使用select进行转换


4. Using select without new, with a transformation

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

And finally, a transformation without the new keyword:

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

这将带给您一些新类型的对象,这些对象是通过转换函数生成的,而不是直接使用new关键字构造的.

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

这篇关于选择新的关键字组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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