EFテーブル連合クエリーjoin

1979 ワード

表が2枚あります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
                    });

            }

        }

  
 
 
 
 
.