简体   繁体   English

了解System.Data.Linq.Table <T>

[英]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查询时, OrderBySelect和其他子句将“附加”到查询并转换为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. 使用whereorderbygroup byselect等建立查询,并在执行查询时将其转换为SQL。

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

相关问题 将System.Data.Linq.Table <T>传递给通用函数 - Passing System.Data.Linq.Table<T> into Generic Function Cast System.Data.Linq.Table <T> 到自定义类 - Cast System.Data.Linq.Table<T> to custom class 将IEnumerable转换为System.Data.Linq.Table - Convert IEnumerable to System.Data.Linq.Table 我如何编写System.Data.Linq.Table的扩展方法 <T> ? - How would I write an extension method for System.Data.Linq.Table<T>? sqlmetal.exe返回值使用通用类型&#39;System.Data.Linq.Table&#39;需要1个类型参数 - sqlmetal.exe returns Using the generic type 'System.Data.Linq.Table' requires 1 type arguments 空表上的System.Data.Linq.DuplicateKeyException - System.Data.Linq.DuplicateKeyException on empty table System.Data.Linq.Mapping修改标记为[Table]的类 - System.Data.Linq.Mapping Modify class marked as [Table] Linq过滤了一个IQueryable <T> (System.Data.Linq.DataQuery)对象由List <T> (System.Collection.Generic.List)对象? - Linq filtering an IQueryable<T> (System.Data.Linq.DataQuery) object by a List<T> (System.Collection.Generic.List) object? System.Linq和System.Data.Linq有什么区别? - What is the difference between System.Linq and System.Data.Linq? C# System.Data.StrongTypingException:列的值'<columnname> ' 在 '<tablename> ' 表是 LINQ 查询中的 DBNull</tablename></columnname> - C# System.Data.StrongTypingException: Value for column '<columnName>' in '<tableName>' table is DBNull in LINQ query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM