Understanding System.Data.Linq.Table<T>

本文关键字:lt gt Table System Data Linq Understanding | 更新日期: 2023-09-27 18:19:22

我在看这个博客:

如果你向下滚动一点这里有一个叫做 DataContext的部分,这里写着

Inside DataClasses1DataContext是一个名为Customers的属性

public System.Data.Linq.Table<Customer> Customers
{
    get
    {
        return this.GetTable<Customer>();
    }
}

然后在右边:

<我>如果您编写一个从数据库检索客户记录的LINQ查询,那么您可以通过此属性访问该数据。它的类型是Table,其中的内部Table类在本例中变成了Customer记录的集合。通常可以这样写代码来访问这个属性:

DataClasses1DataContext db = new DataClasses1DataContext(ConnectionString);
var query = from c in db.Customers
            select c;

这是否意味着在引用属性时,查询并返回整个Customer表?

如果

是这样,如果表中有很多记录,特别是如果您只想检索一个客户,那么这不是有点效率低下吗?

谢谢

Understanding System.Data.Linq.Table<T>

这是否意味着在引用属性时,查询并返回整个Customer表?

No - Table<T>实现IQueryable<T>,这意味着查询延迟到集合被枚举。

Where, OrderBy, Select和其他子句被"附加"到查询上,并在查询被枚举或通过AsEnumerable()转换为linq-to-objects查询时转换为SQL。

请注意,您引用的文章讨论的是Linq-to-SQL,但是linq -to- entities(实体框架)的原理是相同的。使用where, orderby, group by, select等构建查询,并在执行查询时转换为SQL。