有两张表m_Dept、m_User,联合查询

linq方式、EF方式

 

        private void Add()
        {
           List<m_Dept> lst = new List<m_Dept>{
           new m_Dept { DName = "开发", ID = "1" },
           new m_Dept { DName = "测试", ID = "2" },
           new m_Dept { DName = "财务", ID = "3" }
           };

           List<m_User> lstU = new List<m_User>{
           new m_User { UName = "张三", ID = "1"  , DeptID="1"},
           new m_User { UName = "李四", ID = "2" , DeptID="2"},
           new m_User { UName = "王五", ID = "3" , DeptID="1"}
           };

            using(TESTEntities db=new TESTEntities())
            {
                db.m_Dept.AddRange(lst);
                db.m_User.AddRange(lstU);
                db.SaveChanges();
            }
            
        }
        private void getlist()
        {
            using (TESTEntities db = new TESTEntities())
            {

                //linq
                var query = from a in db.m_Dept
                            join b in db.m_User
                            on a.ID equals b.DeptID
                            select new
                            {
                                deptname = a.DName,
                                username = b.UName
                            };
                //ef
                var query2 = db.m_Dept.Join(db.m_User, a => a.ID, b => b.DeptID, (a, b) => new
                    {
                        deptname = a.DName,
                        username = b.UName
                    });

            }

        }

  

 

 

 

 

posted on 2016-01-11 23:27  AmyAlisa  阅读(2767)  评论(0编辑  收藏  举报