注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

午夜阳光

 
 
 

日志

 
 

linq to sql内链接,左右链接(示例)  

2014-08-27 16:53:36|  分类: Linq |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
  public static void InnerJoinTest()//内链接

        {

            DemoDataContext context = new DemoDataContext();

            context.Log = Console.Out;

            //方式一

            var query = from tb1 in context.RoleRow

                        join tb2 in context.RoleFunctionRow on tb1.RoleId equals tb2.RoleId

                        select new { RoleId = tb1.RoleId, RoleName = tb1.EN_Name, FunctionId = tb2.FunctionId };

 

            ObjectDumper.Write(query);

            //方式二

            var query2 =

                 from tb1 in context.RoleRow

                 from tb2 in context.RoleFunctionRow

                 where tb1.RoleId == tb2.RoleId

                 select new { RoleId = tb1.RoleId, RoleName = tb1.EN_Name, FunctionId = tb2.FunctionId };

            ObjectDumper.Write(query2);

 

            //var query3=    from tb1 in context.RoleRow

            //                 join tb2 in context.RoleFunctionRow  on tb1.RoleId equals  tb2.RoleId

            //                 join tb3 in context.FunctionRow  on tb2.FunctionId equals tb3.FunctionId

            //                 select new { RoleId = tb1.RoleId, RoleName = tb1.EN_Name,FunctionId=tb2.FunctionId,FunctionName=tb3.EN_Name }

        }

        public static void LeftJoinTest()//左右链接

        {

            int minRoleId = 1;

            DemoDataContext context = new DemoDataContext();

            context.Log = Console.Out;

            var query = from tb1 in context.RoleRow

                        join tb2 in context.RoleFunctionRow on tb1.RoleId equals tb2.RoleId into tempT

                        from tb3 in tempT.DefaultIfEmpty()// 关键在into tempT from tb3 in tempT.DefaultIfEmpty()

                        where tb1.RoleId > minRoleId

                        select new { RoleId = tb1.RoleId, RoleName = tb1.EN_Name, FunctionId = tb3.FunctionId };

            ObjectDumper.Write(query);

        }

  评论这张
 
阅读(89)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017