LINQ:何时使用“new”在选择子句中?

发布于 2024-12-19 15:28:35 字数 491 浏览 1 评论 0原文

什么时候需要在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事↓在人 2024-12-26 15:28:35

不,如果您选择“新”对象,则 new 不是可选的。 (这就是您正在做的事情。您正在创建具有 2 个属性 FNLN 的匿名类型的新对象)

简单地省略 new 就是语法错误。

如果您有一个想要使用的现有类型(假设您已经定义了一个具有 2 个属性 FNclass Person LN) 那么你就可以使用该类型。例如

var results = from person in people
              select new Person { FN = person.FirstName, LN = person.LastName };

(假设 Person 有一个无参数构造函数,并且 FNLN 是带有 setter 的属性)

唯一可以省略 new 的时候是如果你不是创建一个新对象,例如:

var results = from person in people
              select LN = person.LastName;

您只需选择 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 properties FN and LN)

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 properties FN and LN) then you can use that type. E.g.

var results = from person in people
              select new Person { FN = person.FirstName, LN = person.LastName };

(assuming Person has a parameterless constructor and FN and LN are properties with setters)

The only time you can omit new is if you are not creating a new object, for instance:

var results = from person in people
              select LN = person.LastName;

There you are selecting just the LastName and the result is an IEnumerable<string>

要走就滚别墨迹 2024-12-26 15:28:35

如果您要投影到新类型(匿名类或已知类/结构),则它不是可选的而是必需的。

如果您要投影到现有实例,那么您当然不需要new,即在您的示例中,如果您只想投影到每个人的名字(要获得字符串枚举,您可以这样做:

var results = from person in people select person.FirstName

因此,根据经验,如果您正在构建新类型,则需要在投影中添加新类型(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:

var results = from person in people select person.FirstName

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.

我的黑色迷你裙 2024-12-26 15:28:35

您可以实例化一个命名类型的实例,该实例将 Person 作为参数或使用对象初始值设定项语法赋值,而不是创建匿名类型。

示例:

var results = from person in people
              select new Report(person);

var results = from person in people
              select new Report() { Customer = 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:

var results = from person in people
              select new Report(person);

var results = from person in people
              select new Report() { Customer = person };

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文