C# LINQ查询是否使用运行程序的机器或承载数据库的服务器的处理能力?

C# LINQ查询是否使用运行程序的机器或承载数据库的服务器的处理能力?,c#,sql-server,linq,linq-to-sql,C#,Sql Server,Linq,Linq To Sql,如果我有以下代码 MyDataContext _dataContext = new MyDataContext(MyConnectionString); List<Airplane> entireTable = _dataContext.AirPlanes List<Airplane> propellerPlanes = new List<Airplane>(); foreach(AirPlane p in entireTable){ if(p.I

如果我有以下代码

MyDataContext _dataContext = new MyDataContext(MyConnectionString);

List<Airplane> entireTable = _dataContext.AirPlanes
List<Airplane> propellerPlanes = new List<Airplane>();

foreach(AirPlane p in entireTable){
    if(p.IsPropellerPlane) propellerPlanes.Add(p);
}
MyDataContext\u dataContext=newmydatacontext(MyConnectionString);
List entireTable=\u dataContext.AirPlanes
列表螺旋桨飞机=新列表();
foreach(飞机p在实体中){
如果(p.IsPropellerPlane)为螺旋桨飞机,则添加(p);
}
当这段代码运行时,我假设查询的处理能力来自运行该程序的计算机

但当此代码运行时:

List<Airplane> propellerPlanes = _dataContext.Airplanes.Where(a => a.IsPropellerPlane).ToList();
List螺旋桨飞机=\u dataContext.Airplanes.Where(a=>a.IsPropellerPlane.ToList();

运行查询的处理能力是来自运行该程序的计算机,还是来自我在连接字符串中连接到的安装了SQL Server的计算机?

这取决于查询提供程序。通常,拥有查询提供程序的目的是将查询远程传送到另一台机器。提供者控制着一切。查询的某些部分可能在客户端上运行。LinqtoSQL能够做到这一点。在所有情况下,至少会在提供程序中花费几个周期作为开销或提供诸如实体跟踪之类的功能。

我假设您谈论的是Linq到SQL(还有其他适配器),在这种情况下,Linq到SQL驱动程序在大部分情况下会将Linq查询转换为常规SQL查询


因此,为了回答这个问题,大部分工作将由运行Sql Server的计算机完成。

您是否使用LINQ to Sql?它将lambda表达式转换为SQL语句。您可以使用ToString()查看查询;参见link。在第二种情况下,“查询”能力来自数据库;不过,应用程序服务器仍有一些开销,因为它必须创建SQL语句并将结果绑定到对象(实体)。