Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ - The difference between .Select(n => n.Name) and .Select(n => new { n.Name } );

I am completely new to Linq and wondering if you can help me understand the difference between the following Linq? For example...

//normal select
var contacts = entity.Contacts.Select(n => n.FirstName);

//select new
var contacts2 = entity.Contacts.Select(n => new { n.FirstName });

//normal select output
foreach (var c in contacts)
    Response.Write(c + "<br/>");

//select new output
foreach (var c in contacts2)
    Response.Write(c.FirstName + "<br/>");

The only difference I can see is that in the normal select, the firstname (string) is stored in the collection, whereas in the select new, a contact object is stored in the collecton and the firstname being accessed by its property. Also the select new returns the properties only selected in the statement.

Another difference I noticed is that you can return multiple specific properties in the select new.

In what scenario would you choose one over the other?

Thanks for the help.

like image 603
user971543 Avatar asked Oct 01 '11 14:10

user971543


People also ask

What is the difference between the Select clause and SelectMany () method in LINQ?

Select and SelectMany are projection operators. A select operator is used to select value from a collection and SelectMany operator is used to selecting values from a collection of collection i.e. nested collection.

What is the difference between FirstOrDefault and first?

The major difference between First() and FirstOrDefault() is First will throw an exception when there are no results and FirstOrDefault won't. In fact, FirstOrDefault will simply return the null value (reference types) or the default value of the value type.

What does .Select do in C#?

In a query expression, the select clause specifies the type of values that will be produced when the query is executed. The result is based on the evaluation of all the previous clauses and on any expressions in the select clause itself. A query expression must terminate with either a select clause or a group clause.

What is Select new LINQ?

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


2 Answers

n => n.FirstName gives you a string

n => new { n.FirstName } gives you an anonymous type, with one string property called FirstName

In general, an anonymous type with just one property is probably not what you're looking for, so I'd go for the first option.

like image 93
Richard Ev Avatar answered Oct 18 '22 03:10

Richard Ev


To support Richard Ev's answer:

If you are not familiar with Anonymous types, crack up ildasm and give your exe as an input to it.

You will get something like this:

enter image description here

The thing that you see starting with <>f_AnonymousType() is the one that Richard Ev is talking about. Your syntax of new got translated into a new class (the name was decided by compiler). That is why var keyword is so helpful working with anonymous type.

like image 40
TCM Avatar answered Oct 18 '22 03:10

TCM