In LINQ, what does the select new
keyword combination do?
I haven't found much documentation on this.
Thanks
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. Using select new to return new anonymously typed objects: var records = from person in people select new { person.name, person.id };
The new operator creates a new instance of a type. You can also use the new keyword as a member declaration modifier or a generic type constraint.
@CYB: select new is used when you want your query to create new instances of a certain class, instead of simply taking source items. It allows you to create instances of a completely different class, or even an anonymous class like in OP's case.
Use into keyword in LINQ query to form a group or to continue a query after a select clause. In the above query, the 'into' keyword introduced a new range variable teenStudents, so the first range variable s goes out of scope.
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. Using select new
to return new anonymously typed objects:
var records = from person in people
select new { person.name, person.id };
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. Using select new
to construct "regularly" typed objects:
var records = from person in people
select new CreditRecord(person.creditLimit, person.name);
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 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. Using select
without new
, with no transformation
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. Using select
without new
, with a transformation
And finally, a transformation without the new
keyword:
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 new
keyword to construct it.
It's the select
keyword followed by a new
expression, probably creating an anonymous type.
In other words, it's a combination of the following two unrelated expressions:
select
from a in whatever select 2
new { Prop1 = 3, Prop2 = DateTime.Now }
It's not really a combination, new
just 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.
var q =
from staff in theWhiteHouseStaff
select new { Name = staff.Name } ;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With