LINQ:select vs select new

R

Raj

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

Thank you

Regards
Raj
 
M

Mr. Arnold

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.
 
P

Peter Duniho

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
 
R

RayLopez99

, 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top