user776676
user776676

Reputation: 4385

LINQ: When to use 'new' in Select clause?

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 };

Upvotes: 0

Views: 564

Answers (3)

David Ly
David Ly

Reputation: 31596

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>

Upvotes: 1

Jeff Camera
Jeff Camera

Reputation: 5554

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)

Upvotes: 1

BrokenGlass
BrokenGlass

Reputation: 160912

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.

Upvotes: 1

Related Questions