asp.net MVC + linq to Entity简单教程(五)linq to Entity中join的使用以及子查询

来源:互联网 发布:淘宝wap流量真的没用吗 编辑:程序博客网 时间:2024/04/27 20:13

  多表联查,可以说是我们写sql语句时最常用的一种方法。一般采用,内联(join),左联(left join),右联( right join)的方式。这里不多说了,想必大家也都了解。这里我要说的一点是在linq to Entity 3.5中,是没有左联和右联的,只能一个join。所以你要是想做左联或是右联就得想别的方法。这可能是linq to Entity的不足之处吧,好了,下面我们从从代码来看一下。

  sql语句中,一般的简单联查如下:

      select stu.stu_name,k.parent_mob from ETrain_student as stu       join Etrain_stu_kinded as k       on stu.stu_id=k.stu_id       where stu_schoolcode='sjzsqz'

       linq to Entity的写法如下:

     var stu_kind  = from stu in newEtrain.ETrain_student                           join k in newEtrain.ETrain_stu_kinded on stu.stu_id equals k.stu_id                           where stu.schoolcode == schoolcode                                                  select new stu_kinded                          {                             stuName = stu.stu_name,                             parentPhone = k.parent_mob                          };
     这里要说的一点,on后面的“=”号,必须是equals而且stu在前,k在后,就是join前面的表在前面。顺序不能变。

 如果说,我们on后面的条件是二个怎么办,其实很简单new一下就行。如下

 

var stu_kind  = from stu in newEtrain.ETrain_student                           join k in newEtrain.ETrain_stu_kinded on new{stu.stu_id,stu.schoolcode} equals new{k.stu_id,k.schoolcode}                           where stu.schoolcode == schoolcode                                                  select new stu_kinded                          {                             stuName = stu.stu_name,                             parentPhone = k.parent_mob                          };
是不是很简单呢。呵呵。。。。。。。。。

下面我们说一下子查询。我先上代码,大家看一下,可能会比我说要明白的多

/// <summary>        /// 班级订购人数统计        /// </summary>        /// <param name="id"></param>        /// <returns></returns>        public ActionResult ClassRegNum(string id)        {            var info = from sc in newEtrain.ETrain_school_class                       join grade in newEtrain.ETrain_grade on sc.grade_id equals grade.grade_id                       where sc.schoolcode == id                       orderby sc.grade_id, sc.class_id                       select new ClassRegNum                       {                           grade_class = grade.grade_name + sc.class_Alias,                           StudentNum = (from stu in newEtrain.ETrain_stu_class                  where stu.class_id == sc.class_id && stu.schoolcode == id && stu.grade_id == sc.grade_id select stu).Count(),                           RegSum = (from stuc in newEtrain.ETrain_stu_class                                     join k in newEtrain.ETrain_stu_kindred on stuc.stu_id equals k.stu_id                                     join reg in newEtrain.reg_sms_user on k.parent_mobile equals reg.mobile_number                                     where stuc.schoolcode == id && stuc.grade_id == sc.grade_id && stuc.class_id == sc.class_id && reg.reg_type == 1                                     select stuc).Count()                       };            return View(info);        }

        感觉不用我多说,大家也就明白了,子查询怎么处理了。

        补充一下,我上面的的子查询的例子是把得到的一个数值赋给了变量,可是如果我是一个得到一个字符串的值呢。怎么办呢。其实是一样的,只是在在select后面有一点变化。

        如果我要得到一个学生的姓名, 如下写法。这里注意一点,也是我个人的意见,最好用FirstOrDefault()而不用First()因为前者返回序列中的第一个元素;如果序列中不包含任何元素,则返回默认值。就这一个默认值,就很有用。  

      select stu.stu_name).FirstOrDefault();



到此我想大家对于asp.net MVC + linq to Entity已经有了一些简单的了解,对于一般的操作来说已经没有什么问题。

原创粉丝点击