[英]Understanding System.Data.Linq.Table<T>
I'm looking at this blog : 我在看这个博客 :
If you scroll down a bit there's a section called The DataContext , where it says
如果向下滚动,会有一个名为The DataContext的部分,其中显示
Inside DataClasses1DataContext is property called Customers 在DataClasses1DataContext内部是称为客户的属性
public System.Data.Linq.Table<Customer> Customers
{
get
{
return this.GetTable<Customer>();
}
}
and then right below that: 然后在其下方:
If you write a LINQ query that retrieves customer records from the database, then you can access that data via this property. 如果编写LINQ查询以从数据库中检索客户记录,则可以通过此属性访问该数据。 It is of type Table, where the internal Table class becomes in this case a collection of Customer records.
它是Table类型的,在这种情况下,内部Table类成为客户记录的集合。 One would typically access this property by writing code that looks like this:
通常,可以通过编写如下代码来访问此属性:
DataClasses1DataContext db = new DataClasses1DataContext(ConnectionString);
var query = from c in db.Customers
select c;
Does this mean that at the point the property is referenced the entire Customer table is queried and returned? 这是否意味着在该属性被引用时,将查询并返回整个Customer表?
if so, isn't this a bit inefficient if there's a lot of records in the table, especially if you just want to retrieve one customer? 如果是这样,如果表中有很多记录,尤其是如果您只想检索一位客户,这是否效率不高?
Thanks 谢谢
Does this mean that at the point the property is referenced the entire Customer table is queried and returned?
这是否意味着在该属性被引用时,将查询并返回整个Customer表?
No - Table<T>
implements IQueryable<T>
which means that queries are deferred until the collection is enumerated. 否
IQueryable<T>
Table<T>
实现IQueryable<T>
,这意味着将查询推迟到枚举集合之前。
Where
, OrderBy
, Select
, and other clauses are "attached" to the query and converted to SQL when the query is either enumerated or converted to a linq-to-objects query via AsEnumerable()
. Where
,当枚举查询或通过AsEnumerable()
将查询转换为linq-to-objects查询时, OrderBy
, Select
和其他子句将“附加”到查询并转换为SQL。
Note that the article you reference is discussing Linq-to-SQL, but the principle is the same in Linq-to-Enitites (Entity Framework). 请注意,您参考的文章讨论的是Linq-to-SQL,但原理与Linq-to-Enitites(实体框架)相同。 Queries are build up using
where
, orderby
, group by
, select
, etc. and converted to SQL when the query is executed. 使用
where
, orderby
, group by
, select
等建立查询,并在执行查询时将其转换为SQL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.