bandariiswear

幽艳纷相竞 秾纤各自奇 虽然春烂漫 也要笔扶持 清人题画诗

导航

Linq中怎么查询多张表

View Code
View Code 

#region Group,Join
            //只有join,没有into,内联(inner join)
            //var sql = from c in sdb.Classic
            //          join s in sdb.StuInfo
            //          on c.classID equals s.classID //注意不能用==
            //          select new
            //          {
            //              c.className,
            //              s.stuName
            //          };
            //Console.WriteLine(sql);

            //var sql = from s in sdb.StuInfo
            //          join m in sdb.StuMarks
            //          on s.stuNo equals m.stuNo
            //          into temp                         //many端重新命名
            //          from t in temp.DefaultIfEmpty()   //左连接,DefaultIfEmpty再次遍历右边,缺考人员的成绩默认为null
            //          select new
            //          {
            //              s.stuName,
            //              we = t.writtenExam == null ? 0 : t.writtenExam,
            //              le = t.LabExam == null ? 0 : t.LabExam
            //          };
            //Console.WriteLine(sql);
            //foreach (var item in sql)
            //{
            //    Console.WriteLine("{0}\t{1}\t{2}",item.stuName,item.we,item.le);
            //}

            //三向联接
            //var sql = from c in sdb.Classic
            //          join s in sdb.StuInfo
            //          on c.classID equals s.classID
            //          join m in sdb.StuMarks
            //          on s.stuNo equals m.stuNo
            //          select new
            //          {
            //              c.className,
            //              s.stuName,
            //              we = m.writtenExam == null ? 0 : m.writtenExam,
            //              m.LabExam
            //          };
            //Console.WriteLine(sql);
            //foreach (var item in sql)
            //{
            //    Console.WriteLine("{0}\t{1}\t{2}\t{3}", item.className, item.stuName, item.we, item.LabExam);
            //}

            //双向连接
            var sql = from c in sdb.Classic
                      join s in sdb.StuInfo
                      on c.classID equals s.classID
                      into stu
                      select new
                      {
                          className = c.className,
                          list=stu.ToList<StuInfo>(),
                          count = stu.Count()
                      };
            Console.WriteLine(sql);
            foreach (var item in sql)
            {
                Console.WriteLine("班级:{0},人数:{1}",item.className,item.count);
                foreach (var s in item.list)
                {
                    Console.WriteLine(s.stuName);
                }
            }
            #endregion

http://www.cnblogs.com/jieshaoliujie/archive/2011/12/04/2276091.html

View Code
//多表查询 两种方法生成的SQL语句是一模一样 
//方法一
 
var query1 = from a in db.as 
from b in db.bs.Where(b => b.Id == a.Id)  
from c in db.cs.Where(c => c.Id== b.Id).DefaultIfEmpty()  
from d in db.ds.Where(d => d.Id== c.Id).DefaultIfEmpty()  
select new{}
 
//方法二  
var query2 = from a in db.as 
join b in db.bs on b.Id equals a.Id 
join c in db.cs on c.Id equals b.Id into ac  
join d in db.ds on d.Id equals c.Id into ad 
from c in ac.DefaultIfEmpty() 
from d in ad.DefaultIfEmpty()  

select new{} 

Inner join:
 
var query = from a in db.as 
from b in db.bs
from c in db.cs
where as.Id==bs.Id && bs.Id=cs.Id
select new{}

http://www.cnblogs.com/muarniu/archive/2010/06/29/1767385.html

posted on 2013-02-04 16:21  YEKEYISHUO  阅读(480)  评论(0编辑  收藏  举报