码迷,mamicode.com
首页 > 其他好文 > 详细

EF 表联合查询 join

时间:2016-01-12 01:11:06      阅读:966      评论:0      收藏:0      [点我收藏+]

标签:

有两张表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
                    });

            }

        }

  

 

 

 

 

EF 表联合查询 join

标签:

原文地址:http://www.cnblogs.com/xiaochun126/p/5122952.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!