LinqPad 可以Load一个包含有EF Model的程序集。这样极大的方便调试LINQ.而不需要VisualStudio, 让我们来演示一下。首选创建一个Connection,看下图:

LINQPadConnection

通过点击Browse加载一个程序集,然后你可以看左右的目录树结构:

LinqPad2. 

输入这样的LINQ Expression:

   1:                  from forumGroupMapping in ForumGroupMappings
   2:                   join forumGroup in ForumGroups
   3:                   on forumGroupMapping.ForumGroupId equals forumGroup.Id
   4:   
   5:                   join teamForumGroupMapping in TeamForumGroupMappingAccesses
   6:                   on forumGroupMapping.Id equals teamForumGroupMapping.ForumGroupMappingId
   7:   
   8:                   join teamRole in TeamRoleAgentMappings
   9:                   on teamForumGroupMapping.TeamId equals teamRole.TeamId
  10:   
  11:                   join team in Teams
  12:                   on teamRole.TeamId equals team.Id
  13:   
  14:                   where team.IsActive.Equals(true) &&
  15:                   team.IsDeleted.Equals(false) &&
  16:                   teamRole.IsActive.Equals(true) &&
  17:                   teamRole.IsDeleted.Equals(false) &&
  18:                   teamForumGroupMapping.IsActive.Equals(true) &&
  19:                   teamForumGroupMapping.IsDeleted.Equals(false) &&
  20:                   forumGroupMapping.IsActive.Equals(true) &&
  21:                   forumGroupMapping.IsDeleted.Equals(false) &&
  22:                   forumGroup.IsActive.Equals(true) &&
  23:                   forumGroup.IsActive.Equals(true) &&
  24:                   teamRole.AgentId.Equals(new Guid("476c261f-b25a-4c0b-ba06-f3c9f7b00eeb"))
  25:                  select forumGroupMapping
 
执行后可以看到这个T-SQL:

   1:  SELECT 
   2:  [Filter2].[Id1] AS [Id], 
   3:  [Filter2].[ForumGroupId] AS [ForumGroupId], 
   4:  [Filter2].[ForumId] AS [ForumId], 
   5:  [Filter2].[RootForumId] AS [RootForumId], 
   6:  [Filter2].[RootForumExternalId] AS [RootForumExternalId], 
   7:  [Filter2].[PlatformTypeId] AS [PlatformTypeId], 
   8:  [Filter2].[Locale] AS [Locale], 
   9:  [Filter2].[CreatedBy1] AS [CreatedBy], 
  10:  [Filter2].[CreatedDate1] AS [CreatedDate], 
  11:  [Filter2].[LastUpdatedBy1] AS [LastUpdatedBy], 
  12:  [Filter2].[LastUpdatedDate1] AS [LastUpdatedDate], 
  13:  [Filter2].[IsActive1] AS [IsActive], 
  14:  [Filter2].[IsDeleted1] AS [IsDeleted]
  15:  FROM   (SELECT [Filter1].[Id1], [Filter1].[ForumGroupId], [Filter1].[ForumId], [Filter1].[RootForumId], [Filter1].[RootForumExternalId], [Filter1].[PlatformTypeId], [Filter1].[Locale], [Filter1].[CreatedBy1], [Filter1].[CreatedDate1], [Filter1].[LastUpdatedBy1], [Filter1].[LastUpdatedDate1], [Filter1].[IsActive1], [Filter1].[IsDeleted1], [Filter1].[TeamId1], [Extent4].[TeamId] AS [TeamId2]
  16:      FROM   (SELECT [Extent1].[Id] AS [Id1], [Extent1].[ForumGroupId] AS [ForumGroupId], [Extent1].[ForumId] AS [ForumId], [Extent1].[RootForumId] AS [RootForumId], [Extent1].[RootForumExternalId] AS [RootForumExternalId], [Extent1].[PlatformTypeId] AS [PlatformTypeId], [Extent1].[Locale] AS [Locale], [Extent1].[CreatedBy] AS [CreatedBy1], [Extent1].[CreatedDate] AS [CreatedDate1], [Extent1].[LastUpdatedBy] AS [LastUpdatedBy1], [Extent1].[LastUpdatedDate] AS [LastUpdatedDate1], [Extent1].[IsActive] AS [IsActive1], [Extent1].[IsDeleted] AS [IsDeleted1], [Extent3].[TeamId] AS [TeamId1]
  17:          FROM   [dbo].[ForumGroupMapping] AS [Extent1]
  18:          INNER JOIN [dbo].[ForumGroup] AS [Extent2] ON [Extent1].[ForumGroupId] = [Extent2].[Id]
  19:          INNER JOIN [dbo].[TeamForumGroupMappingAccess] AS [Extent3] ON [Extent1].[Id] = [Extent3].[ForumGroupMappingId]
  20:          WHERE (1 = [Extent1].[IsActive]) AND (0 = [Extent1].[IsDeleted]) AND (1 = [Extent2].[IsActive]) AND (1 = [Extent2].[IsActive]) AND (1 = [Extent3].[IsActive]) AND (0 = [Extent3].[IsDeleted]) ) AS [Filter1]
  21:      INNER JOIN [dbo].[TeamRoleAgentMapping] AS [Extent4] ON [Filter1].[TeamId1] = [Extent4].[TeamId]
  22:      WHERE (1 = [Extent4].[IsActive]) AND (0 = [Extent4].[IsDeleted]) AND (cast('476c261f-b25a-4c0b-ba06-f3c9f7b00eeb' as uniqueidentifier) = [Extent4].[AgentId]) ) AS [Filter2]
  23:  INNER JOIN [dbo].[Team] AS [Extent5] ON [Filter2].[TeamId2] = [Extent5].[Id]
  24:  WHERE (1 = [Extent5].[IsActive]) AND (0 = [Extent5].[IsDeleted])

同时你可以查看这个查询生成IL。这里就不截图了。

LINQPad 支持 C# 4.0 和 Framework 4.0 :

  • LINQ to Objects
  • LINQ to SQL and Entity Framework
  • LINQ to XML
  • Parallel LINQ

您还能查询:

希望这篇POST对您开发有帮助。


作者:Petter Liu
出处:http://www.cnblogs.com/wintersun/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
该文章也同时发布在我的独立博客中-Petter Liu Blog

posted on 2011-07-28 18:38  PetterLiu  阅读(5099)  评论(5编辑  收藏  举报