EF不同查询方法生成的SQL比较
var test1 = db.jj_RolesItem.Join(db.jj_SysItemsClass, m => m.ClassID, g => g.ClassID, (m, g) => new {m.RolesID,g.ClassName }).ToList(); var test2 = db.jj_RolesItem.Include(m => m.jj_SysItemsClass).Select(m => new {m.RolesID,m.jj_SysItemsClass.ClassName }).ToList(); var test3 = from p in db.jj_RolesItem join m in db.jj_SysItemsClass on p.ClassID equals m.ClassID select new { p.RolesID, m.ClassName }; test3.ToList();
前三条生成SQL语句相同
SELECT [Extent1].[RolesID] AS [RolesID], [Extent2].[ClassName] AS [ClassName] FROM [dbo].[jj_RolesItem] AS [Extent1] INNER JOIN [dbo].[jj_SysItemsClass] AS [Extent2] ON [Extent1].[ClassID] = [Extent2].[ClassID]
var test4 = db.jj_RolesItem.First().jj_SysItemsClass.ClassName;
上面这条分两步查询:
SELECT TOP (1) [c].[Roles_Class_ID] AS [Roles_Class_ID], [c].[RolesID] AS [RolesID], [c].[ClassID] AS [ClassID] FROM [dbo].[jj_RolesItem] AS [c]
exec sp_executesql N'SELECT [Extent1].[ClassID] AS [ClassID], [Extent1].[ClassName] AS [ClassName], [Extent1].[ParentID] AS [ParentID], [Extent1].[ParentPath] AS [ParentPath], [Extent1].[Depth] AS [Depth], [Extent1].[Child] AS [Child], [Extent1].[OrderID] AS [OrderID], [Extent1].[Title] AS [Title], [Extent1].[KeyWords] AS [KeyWords], [Extent1].[Description] AS [Description], [Extent1].[Content] AS [Content], [Extent1].[ShowType] AS [ShowType], [Extent1].[LinkUrl] AS [LinkUrl], [Extent1].[Intro] AS [Intro], [Extent1].[ClassPic] AS [ClassPic], [Extent1].[ItemPic] AS [ItemPic], [Extent1].[LanType] AS [LanType], [Extent1].[IsHome] AS [IsHome] FROM [dbo].[jj_SysItemsClass] AS [Extent1] WHERE [Extent1].[ClassID] = @EntityKeyValue1',N'@EntityKeyValue1 int',@EntityKeyValue1=106