LINQ:select vs select new

  • Thread starter Thread starter Raj
  • Start date Start date
R

Raj

What's the significance of using new key word in select statement of LINQ?

Thank you

Regards
Raj
 
Raj said:
What's the significance of using new key word in select statement of LINQ?

Thank you

Regards
Raj

To select a 'New' a Linq Shape whereas, you select the properties of the
object you're querying and only select those properties of the object
for the results of the query instead of selecting all the properties of
the object.


It's like a T-SQL like Select * from table (without the 'New') as
opposed the Select to Select field1, field2 from table is what a 'New'
Shape is about.

That's the only 'New' keyword I know about in Linq.
 
Raj said:
What's the significance of using new key word in select statement of LINQ?

It has the exact same significance as in any other context: it causes a
new instance of an object to be created.

In C# 3.0, along with LINQ anonymous classes were added. These allow
you to instantiate a class in a method without having to declare the
class before-hand. This feature was added to support LINQ, and you'll
most commonly see it used in LINQ expressions, but it's not restricted
to LINQ. You can create an anonymous class any time you like, using the
same syntax used in a LINQ expression.

Likewise, if you do want a named class, you can use "new" in a LINQ
expression to create an instance of that named class, just as you would
create an instance of that named class anywhere else.

Pete
 
, if you do want a named class, you can use "new" in a LINQ
expression to create an instance of that named class, just as you would
create an instance of that named class anywhere else.

Pete

That sounds about right. I noticed they use 'new' when they want to
instantiate something.

I vote for this answer.

RL
 
Back
Top