如何做一个LINQ EX pression知道在哪里()来选择()之前? [英] How does a LINQ expression know that Where() comes before Select()?

查看:124
本文介绍了如何做一个LINQ EX pression知道在哪里()来选择()之前?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个LINQ提供程序。我使用的是引导的 LINQ:建立一个IQueryable提供一系列 的,我已经加C到LINQ的$ C $:建立一个IQueryable提供商 - 第四部分

I'm trying to create a LINQ provider. I'm using the guide LINQ: Building an IQueryable provider series, and I have added the code up to LINQ: Building an IQueryable Provider - Part IV.

我得到它是如何工作的感觉和它背后的理念。现在,我卡上的问题,这是不是一个code的问题,但更多的了解。

I am getting a feel of how it is working and the idea behind it. Now I'm stuck on a problem, which isn't a code problem but more about the understanding.

我发射了这样一句话:

QueryProvider provider = new DbQueryProvider();
Query<Customer> customers = new Query<Customer>(provider);

int i = 3;
var newLinqCustomer = customers.Select(c => new { c.Id, c.Name}).Where(p => p.Id == 2 | p.Id == i).ToList();

不知何故,code,或前pression,知道,其中,来之前的选择。但是,如何以及在哪里?

Somehow the code, or expression, knows that the Where comes before the Select. But how and where?

有没有办法在code表示排序的前pression,其实在调试模式下的ToString(),表明选择来在其中,

There is no way in the code that sorts the expression, in fact the ToString() in debug mode, shows that the Select comes before the Where.

我试图让code失败。一般我做了其中,先升后的选择

I was trying to make the code fail. Normal I did the Where first and then the Select.

那么,如何前pression排序呢?我没有做过的指南任何改变code。

So how does the expression sort this? I have not done any change to the code in the guide.

推荐答案

这位前pressions是PTED跨$ P $的顺序,翻译或执行你写他们 - 这样其中,做的不可以来之前的选择

The expressions are "interpreted", "translated" or "executed" in the order you write them - so the Where does not come before the Select

如果您执行:             VAR newLinqCustomer = customers.Select(C =>新{c.Id,c.Name}),其中(P => p.Id == 2 | p.Id == I).ToList();

If you execute: var newLinqCustomer = customers.Select(c => new { c.Id, c.Name}).Where(p => p.Id == 2 | p.Id == i).ToList();

那么其中,的IEnumerable 的IQueryable 匿名类型。

如果您执行:

        var newLinqCustomer = customers.Where(p => p.Id == 2 | p.Id == i).Select(c => new { c.Id, c.Name}).ToList();

那么其中,的IEnumerable 的IQueryable 客户类型。

我唯一能想到的是,也许你看到一些生成的SQL所在的SELECT和WHERE已被重新排序?在这种情况下,我猜想,有一个优化步骤中的某处(例如) LINQ&NBSP;到&NBSP; SQL 提供商需要 SELECT ID,姓名FROM(SELECT识别码,从客户名称其中id = 2 ||编号= @ I),并将其转换成 SELECT识别码,从客户名称WHERE ID = 2 || ID = @ I - 但这必须是一个提供具体的优化

The only thing I can think of is that maybe you're seeing some generated SQL where the SELECT and WHERE have been reordered? In which case I'd guess that there's an optimisation step somewhere in the (e.g.) LINQ to SQL provider that takes SELECT Id, Name FROM (SELECT Id, Name FROM Customer WHERE Id=2 || Id=@i) and converts it to SELECT Id, Name FROM Customer WHERE Id=2 || Id=@i - but this must be a provider specific optimisation.

这篇关于如何做一个LINQ EX pression知道在哪里()来选择()之前?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆