Linq 的IQueryable和IEnumerable方式


IEnumerable方式:

public IEnumerable<WebManageUsers> GetWebManageUsers(ISpecification<WebManageUsers> specification)
{
var linq = base.GetEntities(specification).AsQueryable()
.Include(i => i.WebManageUser_WebManageRoles)
.AsQueryable(); //include会增加链库操作
return linq;
}

解释成sql:
exec sp_executesql N'SELECT
[Project1].[ManageUserID] AS [ManageUserID],
[Project1].[LoginName] AS [LoginName],
[Project1].[Password] AS [Password],
[Project1].[RealName] AS [RealName],
[Project1].[Gender] AS [Gender],
[Project1].[Phone] AS [Phone],
[Project1].[Mobile] AS [Mobile],
[Project1].[Email] AS [Email],
[Project1].[AvatarUrl] AS [AvatarUrl],
[Project1].[QQ] AS [QQ],
[Project1].[MSN] AS [MSN],
[Project1].[Rtx] AS [Rtx],
[Project1].[Birthday] AS [Birthday],
[Project1].[Description] AS [Description],
[Project1].[DepartmentID] AS [DepartmentID],
[Project1].[CreateDate] AS [CreateDate],
[Project1].[UpdateDate] AS [UpdateDate],
[Project1].[Operator] AS [Operator],
[Project1].[Status] AS [Status],
[Project1].[IntroductionURL] AS [IntroductionURL],
[Project1].[WebSystemID] AS [WebSystemID],
[Project1].[C1] AS [C1],
[Project1].[ManageUserID1] AS [ManageUserID1],
[Project1].[ManageRoleID] AS [ManageRoleID],
[Project1].[CreateDate1] AS [CreateDate1]
FROM ( SELECT
[Extent1].[ManageUserID] AS [ManageUserID],
[Extent1].[LoginName] AS [LoginName],
[Extent1].[Password] AS [Password],
[Extent1].[RealName] AS [RealName],
[Extent1].[Gender] AS [Gender],
[Extent1].[Phone] AS [Phone],
[Extent1].[Mobile] AS [Mobile],
[Extent1].[Email] AS [Email],
[Extent1].[AvatarUrl] AS [AvatarUrl],
[Extent1].[QQ] AS [QQ],
[Extent1].[MSN] AS [MSN],
[Extent1].[Rtx] AS [Rtx],
[Extent1].[Birthday] AS [Birthday],
[Extent1].[Description] AS [Description],
[Extent1].[DepartmentID] AS [DepartmentID],
[Extent1].[CreateDate] AS [CreateDate],
[Extent1].[UpdateDate] AS [UpdateDate],
[Extent1].[Operator] AS [Operator],
[Extent1].[Status] AS [Status],
[Extent1].[IntroductionURL] AS [IntroductionURL],
[Extent1].[WebSystemID] AS [WebSystemID],
[Extent2].[ManageUserID] AS [ManageUserID1],
[Extent2].[ManageRoleID] AS [ManageRoleID],
[Extent2].[CreateDate] AS [CreateDate1],
CASE WHEN ([Extent2].[ManageUserID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
FROM [dbo].[WebManageUsers] AS [Extent1]
LEFT OUTER JOIN [dbo].[WebManageUser_WebManageRoles] AS [Extent2] ON [Extent1].[ManageUserID] = [Extent2].[ManageUserID]
WHERE (@p__linq__0 = 1) AND ([Extent1].[CreateDate] > (CASE WHEN (@p__linq__1 IS NULL) THEN @p__linq__2 ELSE @p__linq__1 END)) AND ([Extent1].[CreateDate] < (CASE WHEN (@p__linq__3 IS NULL) THEN @p__linq__4 ELSE @p__linq__3 END))
) AS [Project1]
ORDER BY [Project1].[ManageUserID] ASC, [Project1].[C1] ASC',N'@p__linq__0 bit,@p__linq__1 datetime2(7),@p__linq__2 datetime2(7),@p__linq__3 datetime2(7),@p__linq__4 datetime2(7)',@p__linq__0=1,@p__linq__1=NULL,@p__linq__2='0001-01-01 00:00:00',@p__linq__3=NULL,@p__linq__4='9999-12-31 23:59:59.9999999'

 

 


IQueryable方式:

   public IQueryable<WebManageUsers> GetWebManageUsers(ISpecification<WebManageUsers> specification)
{
var linq = base.GetEntities(specification).AsQueryable()
.Include(i => i.WebManageUser_WebManageRoles)
.AsQueryable(); //include会增加链库操作
return linq;
}
解释成sql:
exec sp_executesql N'SELECT
[Extent1].[ManageUserID] AS [ManageUserID],
[Extent1].[CreateDate] AS [CreateDate],
[Extent1].[RealName] AS [RealName]
FROM [dbo].[WebManageUsers] AS [Extent1]
WHERE (@p__linq__0 = 1) AND ([Extent1].[CreateDate] > (CASE WHEN (@p__linq__1 IS NULL) THEN @p__linq__2 ELSE @p__linq__1 END)) AND ([Extent1].[CreateDate] < (CASE WHEN (@p__linq__3 IS NULL) THEN @p__linq__4 ELSE @p__linq__3 END))',N'@p__linq__0 bit,@p__linq__1 datetime2(7),@p__linq__2 datetime2(7),@p__linq__3 datetime2(7),@p__linq__4 datetime2(7)',@p__linq__0=1,@p__linq__1=NULL,@p__linq__2='0001-01-01 00:00:00',@p__linq__3=NULL,@p__linq__4='9999-12-31 23:59:59.9999999'


以上说明:

IQueryable才是linq的延时加载
啥时候用:
1 结果集很大,返回的字段可能需要组装
2 使用 result = result.Skip(pageSize * (pageIndex - 1)).Take(pageSize);分页时

 

posted @ 2016-06-22 16:09  那就让我这样吧  阅读(400)  评论(0编辑  收藏  举报