博客园  :: 首页  :: 新随笔  :: 联系 :: 管理

ActiveRecord多表查询

Posted on 2011-03-31 09:17  codingsilence  阅读(178)  评论(0编辑  收藏  举报

当from中有多个表时,而select 中只需要一个表中的所有数据,不必像sql一样用*,只要把类名写出来就行,摸索出来的经验,记下,希望对大家有帮助.

见下面查询,特别是加红部分.

       [DataObjectMethod(DataObjectMethodType.Select)]
        public static ZjcXueshenghuojiang[] List(int xuenian, int xueqi,long banjiId,long banjiStudentId)
        {
            //BaseBanjiStudent stu = new BaseBanjiStudent();
            //stu.BanjiStudentid

            string strHql = @"Select huojiang from ZjcXueshenghuojiang huojiang , BaseBanjiStudent stu ";
            strHql += " where huojiang.Xuenian = :xuenian and huojiang.Xueqi = :xueqi ";
            strHql += " and  huojiang.BanjiStudentId = stu.BanjiStudentid  and stu.BaseBanji.Banjiid = :banjiId ";
            //    strHql += " order by huojiang.StartDate DESC ";
            SimpleQuery query = new SimpleQuery(
                typeof(ZjcXueshenghuojiang), strHql);
            query.SetParameter("xuenian", xuenian);
            query.SetParameter("xueqi", xueqi);
            query.SetParameter("banjiId", banjiId);
            return (ZjcXueshenghuojiang[])ExecuteQuery(query);
        }