Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use "select new " in LINQ

I am very new to LINQ to SQL, so please forgive me if its a layman sort of question.

I see at many places that we use "select new" keyword in a query. For e.g.

var orders =  from o in db.Orders select new {
                o.OrderID,
                 o.CustomerID,
                 o.EmployeeID,
                 o.ShippedDate
           }

Why don't we just remove select new and just use "select o"

var orders =  from o in db.Orders select o;

What I can differentiate is performance difference in terms of speed, i.e. then second query will take more time in execution than the first one.

Are there any other "differences" or "better to use" concepts between them ?

like image 716
Marshal Avatar asked Sep 04 '11 05:09

Marshal


People also ask

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.

What is the use of select in LINQ?

The Select clause is used to shape the data. You can select the whole object as it is or only some properties of it. In the above example, we selected the each resulted string elements. In the following example, we use LINQ query syntax to find out teenager students from the Student collection (sequence).

Why select comes after from in LINQ?

The reason is, LINQ is used with C# or other programming languages, which requires all the variables to be declared first. From clause of LINQ query just defines the range or conditions to select records. So that's why from clause must appear before Select in LINQ.

Does LINQ select create new object?

The type of sequence returned by a query is determined by the type of value returned by the select clause. LINQ select can return a sequence that contains elements created during the execution of the query.


3 Answers

With the new keyword they are building an anonymous object with only those four fields. Perhaps Orders has 1000 fields, and they only need 4 fields.

If you are doing it in LINQ-to-SQL or Entity Framework (or other similar ORMs) the SELECT it'll build and send to the SQL Server will only load those 4 fields (note that NHibernate doesn't exactly support projections at the db level. When you load an entity you have to load it completely). Less data transmitted on the network AND there is a small chance that this data is contained in an index (loading data from an index is normally faster than loading from the table, because the table could have 1000 fields while the index could contain EXACTLY those 4 fields).

The operation of selecting only some columns in SQL terminology is called PROJECTION.

A concrete case: let's say you build a file system on top of SQL. The fields are:

  • filename VARCHAR(100)
  • data BLOB

Now you want to read the list of the files. A simple SELECT filename FROM files in SQL. It would be useless to load the data for each file while you only need the filename. And remember that the data part could "weight" megabytes, while the filename part is up to 100 characters.

After reading how much "fun" is using new with anonymous objects, remember to read what @pleun has written, and remember: ORMs are like icebergs: 7/8 of their working is hidden below the surface and ready to bite you back.

like image 170
xanatos Avatar answered Sep 21 '22 01:09

xanatos


The answer given is fine, however I would like to add another aspect.

Because, using the select new { }, you disconnect from the datacontext and that makes you loose the change tracking mechanism of Linq-to-Sql.

So for only displaying data, it is fine and will lead to performance increase.

BUT if you want to do updates, it is a no go.

like image 40
Pleun Avatar answered Sep 21 '22 01:09

Pleun


In the select new, we're creating a new anonymous type with only the properties you need. They'll all get the property names and values from the matching Orders. This is helpful when you don't want to pull back all the properties from the source. Some may be large (think varchar(max), binary, or xml datatypes), and we might want to exclude those from our query.

If you were to select o, then you'd be selecting an Order with all its properties and behaviours.

like image 21
p.campbell Avatar answered Sep 21 '22 01:09

p.campbell