LINQ:何时使用“new”在选择子句中?
什么时候需要在 select 子句中使用 new ,如下例所示?谢谢。
var results = from person in people
select new { FN = person.FirstName, LN = person.LastName };
我已经搜索并部分理解了这一点:“在 select new 中,我们正在创建一个新的匿名类型,仅包含您需要的属性。”但这是否意味着我可以省略 new 而不会获得匿名类型?
用另一种方式重申我的问题:“新”是可选的吗?这里的“new”是否与 C# new 类似,但值类型不需要它?
我可以这样做吗?
var results = from person in people
select { FN = person.FirstName, LN = person.LastName };
When is it required to use new in the select clause as in the following example? Thanks.
var results = from person in people
select new { FN = person.FirstName, LN = person.LastName };
I've searched and partially understand this: "In the select new, we're creating a new anonymous type with only the properties you need." But does it mean I can omit new and not get an anonymous type?
To restate my question in another way: is 'new' optional? Is 'new' here similar to C# new where it is not required with value type?
Can I do this?
var results = from person in people
select { FN = person.FirstName, LN = person.LastName };
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,如果您选择“新”对象,则
new
不是可选的。 (这就是您正在做的事情。您正在创建具有 2 个属性FN
和LN
的匿名类型的新对象)简单地省略
new
就是语法错误。如果您有一个想要使用的现有类型(假设您已经定义了一个具有 2 个属性
FN
和的
) 那么你就可以使用该类型。例如class
Person
LN(假设 Person 有一个无参数构造函数,并且
FN
和LN
是带有 setter 的属性)唯一可以省略
new
的时候是如果你不是创建一个新对象,例如:您只需选择
LastName
,结果是IEnumerable
No,
new
is not optional if you're selecting "new" objects. (which is what you are doing. You are creating new objects of an anonymous type with 2 propertiesFN
andLN
)Simply omitting
new
is a syntax error.If you have an existing type you would like to use, (say you've defined a
class
Person
with 2 propertiesFN
andLN
) then you can use that type. E.g.(assuming Person has a parameterless constructor and
FN
andLN
are properties with setters)The only time you can omit
new
is if you are not creating a new object, for instance:There you are selecting just the
LastName
and the result is anIEnumerable<string>
如果您要投影到新类型(匿名类或已知类/结构),则它不是可选的而是必需的。
如果您要投影到现有实例,那么您当然不需要
new
,即在您的示例中,如果您只想投影到每个人的名字(要获得字符串枚举,您可以这样做:因此,根据经验,如果您正在构建新类型,则需要在投影中添加新类型(
Select
),如果您只是选择已经存在的内容,则不需要't。If you are projecting to a new type (anonymous class or known class/struct) it is not optional but required.
If you are projecting to an existing instance though of course you don't need the
new
, i.e. in your example if you just wanted to project to the first name of each person (to get a string enumeration you could just do:So rule of thumb if you are constructing a new type you need new in the projection (
Select
), if you are just selecting something that already exists you don't.您可以实例化一个命名类型的实例,该实例将 Person 作为参数或使用对象初始值设定项语法赋值,而不是创建匿名类型。
示例:
编辑:或者当然只需从 person 对象中选择一个属性,如 BrokenGlass 指出的那样!
编辑:我刚刚重新阅读了您的问题,并想指出 C# 中的匿名类型只是具有编译器生成的类型名称的普通类型。所以是的,您仍然需要使用 new 运算符,因为您正在创建类型的新实例。 匿名类型(C# 编程指南)
Instead of creating an anonymous type you can instantiate an instance of a named type that takes a Person as an argument or assign values using the object initializer syntax.
Example:
Edit: Or of course just select a single property from the person object as BrokenGlass pointed out!
Edit: I just re-read your question and wanted to point out that anonymous types in C# are just normal types with the type name generated by the compiler. So yes, you still need to use the new operator because you're creating a new instance of a type. Anonymous Types (C# Programming Guide)