LINQ,join的时候,会把所有字段都select出来,然后再join的吗?
我的环境是EF6,VS2017,Mysql,winform
我最近使用join进行三表连接的时候,发现生成的SQL语句中,是首先将join的表的所有字段select后,再套用一个select检索需要的字段。为此,我还测试了再join前先用select选择需要的字段,然后再join,也没有起作用。
比如
生成的SQL语句是这样的:
SQL语句中,把m_codens和m_user的字段都select出来了,这显然没有必要。。
另外,最外层的select似乎也没有必要?已经在第二个select中,将数据都检索出来了。
我最近使用join进行三表连接的时候,发现生成的SQL语句中,是首先将join的表的所有字段select后,再套用一个select检索需要的字段。为此,我还测试了再join前先用select选择需要的字段,然后再join,也没有起作用。
比如
Dim rsSql = From coden In db.m_codens
Where coden.companyid = 11 And
coden.codeid = 99 And
coden.code_n = 1 And
coden.code_s = "1"
Join user In db.m_user On coden.data1 Equals CStr(user.userid) And
user.companyid Equals coden.companyid
Join log In db.d_log_operation On user.userid Equals log.userid
Where log.pcno = 1
Order By log.srdate, log.srtime
Select user.userid, user.usename, log.srdate, log.srtime, log.inputtext
生成的SQL语句是这样的:
SELECT
`Project1`.`C1`,
`Project1`.`userid`,
`Project1`.`username`,
`Project1`.`srdate`,
`Project1`.`srtime`,
`Project1`.`inputtext`
FROM (SELECT
`Filter1`.`userid`,
`Filter1`.`username`,
`Extent3`.`srdate`,
`Extent3`.`srtime`,
`Extent3`.`inputtext`,
1 AS `C1`
FROM (SELECT
`Extent1`.`id`,
`Extent1`.`companyid `,
`Extent1`.`codeid`,
`Extent1`.`code_n`,
`Extent1`.`code_s`,
`Extent1`.`data1`,
`Extent1`.`data2`,
`Extent1`.`data3`,
`Extent1`.`data4`,
`Extent1`.`revison`,
`Extent1`.`timestamp`,
`Extent2`.`id` AS `ID1`,
`Extent2`.`companyid ` AS `companyid 1`,
`Extent2`.`userid`,
`Extent2`.`username`,
`Extent2`.`passwd`,
`Extent2`.`superkbn`,
`Extent2`.`revison` AS `REVISON1`,
`Extent2`.`timestamp` AS `TIMESTAMP1`
FROM `m_codens` AS `Extent1` INNER JOIN `m_user` AS `Extent2` ON ((`Extent1`.`data1` = (`Extent2`.`userid`)) OR ((`Extent1`.`data1` IS NULL) AND (`Extent2`.`userid` IS NULL))) AND (`Extent1`.`companyid ` = `Extent2`.`companyid `)
WHERE ((99 = `Extent1`.`codeid`) AND (1 = `Extent1`.`code_n`)) AND (@gp1 = `Extent1`.`code_s`)) AS `Filter1` INNER JOIN `d_log_operation` AS `Extent3` ON (`Filter1`.`userid` = `Extent3`.`userid`) OR ((`Filter1`.`userid` IS NULL) AND (`Extent3`.`userid` IS NULL))
WHERE (`Filter1`.`companyid ` = @p__linq__0) AND (1 = `Extent3`.`regno`)) AS `Project1`
ORDER BY
`Project1`.`srdate` ASC,
`Project1`.`srtime` ASC
SQL语句中,把m_codens和m_user的字段都select出来了,这显然没有必要。。
另外,最外层的select似乎也没有必要?已经在第二个select中,将数据都检索出来了。
...全文
相关推荐
LINQ_to_SQL语法及实例大全.doc
除了上述内容,LINQ to SQL还支持Join、GroupBy、OrderBy、OrderByDescending等操作,以及更复杂的查询构造。
LINQ_to_SQL语法及实例大全
### LINQ to SQL语法及实例大全#### 一、LINQ to SQL简介**LINQ to SQL** 是 Microsoft .NET Framework 提供的一种用于在数据库和应用程序之间进行数据操作的技术
完整linq语法
##### 3.2 LINQ查询表达式详解接下来,我们来看具体的LINQ查询表达式:```csharpvar a = from a in dt1 join b in dt2 on new { a.Field
LINQ To SQL 语法及实例大全
条件形式**```csharpvar query = from p in db.Products select p.Price > 100 ?
Linq to datable(Left join right join full join)实例
对于那些没有匹配的记录,结果集中相应字段将为NULL。


LINQ
