首页 新闻 会员 周边 捐助

EF使用join统计总数遇到诡异的问题。

0
悬赏园豆:20 [待解决问题]
var query1 = from c in db.Catalogue
                            join l in db.Live on c.LiveId equals l.Id 
                            where c.StartDate < date
                            select new{ c, l};

                total = query1.Count();//生成的sql语句无join
                var list = query1.ToList();//生成的sql语句有join

count()后生成的语句

SELECT 
    [GroupBy1].[A1] AS [C1]
    FROM ( SELECT 
        COUNT(1) AS [A1]
        FROM [dbo].[Live_Catalogue] AS [Extent1]
        WHERE [Extent1].[StartDate] <  '2016/8/19 16:24:06'
    )  AS [GroupBy1]

 

ToList()生成的语句

SELECT     [Extent1].[Id] AS [Id], 
    [Extent1].[LiveId] AS [LiveId], 
    [Extent1].[ChapterId] AS [ChapterId], 
    [Extent1].[RoomId] AS [RoomId], 
    [Extent1].[TeacherId] AS [TeacherId], 
    [Extent1].[Title] AS [Title], 
    [Extent1].[ParentId] AS [ParentId], 
    [Extent1].[StartDate] AS [StartDate], 
    [Extent1].[EndDate] AS [EndDate], 
    [Extent1].[OrderId] AS [OrderId], 
    [Extent1].[ClientNum] AS [ClientNum], 
    [Extent1].[VideoUrl] AS [VideoUrl], 
    [Extent2].[Id] AS [Id1], 
    [Extent2].[Title] AS [Title1], 
    [Extent2].[SubscriptionNum] AS [SubscriptionNum], 
    [Extent2].[ImageUrl] AS [ImageUrl], 
    [Extent2].[TeacherId] AS [TeacherId1], 
    [Extent2].[QQGroup] AS [QQGroup], 
    [Extent2].[QQGroupUrl] AS [QQGroupUrl], 
    [Extent2].[QQCodeUrl] AS [QQCodeUrl], 
    [Extent2].[MyClassId] AS [MyClassId], 
    [Extent2].[Introduction] AS [Introduction], 
    [Extent2].[Content] AS [Content], 
    [Extent2].[FitCrowd] AS [FitCrowd], 
    [Extent2].[IsEnabled] AS [IsEnabled], 
    [Extent2].[ClassId] AS [ClassId], 
    [Extent2].[RoomId] AS [RoomId1], 
    [Extent2].[IsFree] AS [IsFree], 
    [Extent2].[IsSingleDirectory] AS [IsSingleDirectory]
    FROM  [dbo].[Live_Catalogue] AS [Extent1]
    INNER JOIN [dbo].[Live] AS [Extent2] ON [Extent1].[LiveId] = [Extent2].[Id] 
        WHERE [Extent1].[StartDate] < '2016/8/19 16:24:06'

 2个表都没设置主外键关系

为什么Count()出的SQL语句没有生成JOIN

sky_net的主页 sky_net | 初学一级 | 园豆:10
提问于:2016-08-19 17:30
< >
分享
所有回答(1)
0

你应该是看错了

jello chen | 园豆:7336 (大侠五级) | 2016-08-20 07:43

  这绝不是看错了。

支持(0) 反对(0) sky_net | 园豆:10 (初学一级) | 2016-08-20 17:11

@sky_net: [dbo].[Live_Catalogue]这个表是哪里来的

支持(0) 反对(0) jello chen | 园豆:7336 (大侠五级) | 2016-08-20 22:31

@jello chen: 一个主表一个从表

支持(0) 反对(0) sky_net | 园豆:10 (初学一级) | 2016-08-22 07:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册