简体   繁体   English

为什么在LINQ中使用“select new”

[英]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. 我对LINQ to SQL很新,所以请原谅我,如果它是一个外行问题。

I see at many places that we use "select new" keyword in a query. 我在许多地方看到我们在查询中使用"select new"关键字。 For eg 例如

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" 为什么我们不删除select new,只使用"select o"

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

What I can differentiate is performance difference in terms of speed, ie 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 ? 它们之间是否存在其他“差异”或“更好用”的概念?

With the new keyword they are building an anonymous object with only those four fields. 使用new关键字,他们正在构建一个只包含这四个字段的匿名对象。 Perhaps Orders has 1000 fields, and they only need 4 fields. 也许Orders有1000个字段,它们只需要4个字段。

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). 如果您在LINQ-to-SQL或Entity Framework(或其他类似的ORM)中执行它,它将构建并发送到SQL Server的SELECT将仅加载这4个字段(请注意,NHibernate并不完全支持在db level。加载实体时,必须完全加载它。 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). 在网络上传输的数据较少并且此数据包含在索引中的可能性很小(从索引加载数据通常比从表中加载数据更快,因为该表可能有1000个字段,而索引可以包含这些数据4字段)。

The operation of selecting only some columns in SQL terminology is called PROJECTION . 在SQL术语中仅选择一些列的操作称为PROJECTION

A concrete case: let's say you build a file system on top of SQL. 具体案例:假设你在SQL之上构建一个文件系统。 The fields are: 这些领域是:

  • filename VARCHAR(100) filename VARCHAR(100)
  • data BLOB 数据BLOB

Now you want to read the list of the files. 现在您要读取文件列表。 A simple SELECT filename FROM files in SQL. SQL中的一个简单的SELECT filename FROM files It would be useless to load the data for each file while you only need the filename . 在只需要filename时加载每个文件的data是没用的。 And remember that the data part could "weight" megabytes, while the filename part is up to 100 characters. 请记住, data部分可以“加权”兆字节,而filename部分最多可以包含100个字符。

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. 在阅读了匿名对象使用new之后有多么“有趣”之后,记得阅读@pleun写的内容,并记住:ORM就像冰山一样:他们工作的7/8隐藏在表面之下并准备咬你。

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. 因为,使用select new { } ,您断开与datacontext的连接,这使您失去了 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. 但如果你想做更新,那就不行了。

In the select new , we're creating a new anonymous type with only the properties you need. select new ,我们创建了一个只包含您需要的属性的新匿名类型。 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. 有些可能很大(想想varchar(max)binaryxml数据类型),我们可能希望从查询中排除这些数据类型。

If you were to select o , then you'd be selecting an Order with all its properties and behaviours. 如果您要select o ,那么您将选择包含其所有属性和行为的Order

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM