简体   繁体   English

从Entity Framework中的视图中选择

[英]Selecting from a view in Entity Framework

I was trying to select some columns from a view with the direct way like the below code snippet 我试图用直接的方式从视图中选择一些列,如下面的代码片段

var q = new TDSViewConnection();
var trials = q.progressive_filtering_lookup_decoded_v
              .Where(z => z.source == "") 
              .Select(z => z.trial_id);

The SQL statement generated for the above expression is like as below 为上面的表达式生成的SQL语句如下所示

SELECT 
    [Extent1].[trial_id] AS [trial_id]
FROM 
    (SELECT 
         [progressive_filtering_lookup_decoded_v].[master_protocol_id] AS [master_protocol_id], 
         [progressive_filtering_lookup_decoded_v].[trial_id] AS [trial_id], 
         [progressive_filtering_lookup_decoded_v].[source] AS [source], 
         [progressive_filtering_lookup_decoded_v].[discipline_code] AS [discipline_code], 
         [progressive_filtering_lookup_decoded_v].[crop_team_code] AS [crop_team_code], 
         [progressive_filtering_lookup_decoded_v].[crop_name] AS [crop_name], 
         [progressive_filtering_lookup_decoded_v].[pest_name] AS [pest_name], 
         [progressive_filtering_lookup_decoded_v].[country_code] AS [country_code], 
         [progressive_filtering_lookup_decoded_v].[year] AS [year]
     FROM 
         [dbo].[progressive_filtering_lookup_decoded_v] AS [progressive_filtering_lookup_decoded_v]) AS [Extent1]
WHERE 
    N'' = [Extent1].[source]

The question is why are there two select statements? 问题是为什么会有两个选择语句? I think it should be only one statement. 我认为这只是一个陈述。

As the other user posted, your resultant query is a projection result, or a subset of the full query. 当其他用户发布时,您的结果查询是一个投影结果或完整查询的子集。 As such, the query engine will treat the full object as the source, and will simply select the items that you want from it, thus resulting in the sub-query. 这样,查询引擎会将整个对象视为源,并且只需从中选择所需的项目,从而生成子查询。

If you look at the query plan performance within SQL Server, there is typically no real difference between running the query the way that EF does the query and the way that you might expect to see it. 如果您查看SQL Server中的查询计划性能,则以EF进行查询的方式运行查询与期望看到查询的方式之间通常没有真正的区别。

The key benefit here is that since you only want the one column, EF makes sure that is all that is returned from the database. 这里的主要好处是,由于只需要一个列,因此EF确保这是从数据库返回的所有内容。 Typically I find that ensuring you have the smallest result set is the desired function. 通常,我发现确保您拥有最小的结果集是所需的功能。 This becomes even more apparent with more complex projections. 随着更复杂的预测,这一点变得更加明显。

With Entity Framework, you will get a SQL generation under the covers of what your LINQ statement expresses. 使用Entity Framework,您将在LINQ语句表示的内容的掩护下获得SQL生成。 It appears to me that your LINQ statement is performing it's first select based on the preliminary portion of your code, and then when you once again use the '.Select' keyword, you are essentially performing a sub-query on the result set that you previously retrieved. 在我看来,您的LINQ语句正在执行,它首先是基于代码的初始部分进行选择,然后再次使用'.Select'关键字时,实际上是在对您要返回的结果集执行子查询先前检索到的。

In other words: var trials = q.progressive_filtering_lookup_decoded_v.Where(z => z.source == "") Is the initial query, which will return a result set. 换句话说: var trials = q.progressive_filtering_lookup_decoded_v.Where(z => z.source == "")是初始查询,它将返回结果集。

Then you are running .Select(z => z.trial_id); 然后您正在运行.Select(z => z.trial_id); which runs a separate query using your first result set as a starting point. 它以您的第一个结果集为起点运行单独的查询。

This is why you are seeing two select statements in your generated SQL. 这就是为什么在生成的SQL中看到两个select语句的原因。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM