castle activerecord mysql_Castle ActiveRecord学习(四)延迟加载、分页查询、where条件...

一、延迟加载

//用户发布的主题,一对多;Table:外键表;ColumnKey:外键;Lazy:延迟加载;Cascade:级联操作(级联删除)

[HasMany(typeof(ThemeInfo), Table = "ThemeInfo", ColumnKey = "UserID", Lazy = true, Cascade =ManyRelationCascadeEnum.Delete)]public virtual IList ThemeInfos { get; set; }

Lazy=true,当需要用到的时候才会自动读取,用法:

//延迟加载,必须使用 using (new SessionScope())

using (newSessionScope())

{

StringBuilder htmlStr1= newStringBuilder();

IList list1 =Models.UserInfo.FindAll();foreach (Models.UserInfo item inlist1)

{

htmlStr1.Append("编号:" + item.ID + "名称:" + item.Name + "
");

htmlStr1.Append("    我的主题:
");foreach (Models.ThemeInfo item2 initem.ThemeInfos)

{

htmlStr1.Append("      编号:" + item2.ID + "名称:" + item2.Subject + "
");

}

}

Literal1.Text=htmlStr1.ToString();int PageCount = 0;

StringBuilder htmlStr2= newStringBuilder();

IList list2 = Models.UserInfo.FindInPage(2, 3, refPageCount);foreach (Models.UserInfo item inlist2)

{

htmlStr2.Append("编号:" + item.ID + "名称:" + item.Name + "
");

htmlStr2.Append("    我的主题:
");foreach (Models.ThemeInfo item2 initem.ThemeInfos)

{

htmlStr2.Append("      编号:" + item2.ID + "名称:" + item2.Subject + "
");

}

}

Literal2.Text=htmlStr2.ToString();

Literal3.Text= PageCount + "";

}

分页查询:

//分页获取数据

public static IList FindInPage(int PageIndex, int PageSize, ref int PageCount)

{

int BeginIndex = ((PageIndex - 1) * PageSize);

string hql = " from UserInfo c ";

SimpleQuery query = new SimpleQuery(hql);

query.SetQueryRange(BeginIndex, PageSize);//设置从哪一条开始

ScalarQuery squery = new ScalarQuery(typeof(UserInfo), "select Count(*) from UserInfo");

int recordCount = Convert.ToInt32(ExecuteQuery(squery));//获得总条数

PageCount = (recordCount / PageSize) + (recordCount % PageSize > 0 ? 1 : 0);

return query.Execute();

}

二、使用Where子句

[HasMany(typeof(ThemeInfo), Table = "ThemeInfo", ColumnKey = "UserID", Lazy = true, Cascade = ManyRelationCascadeEnum.Delete,Where="UserID=1")]

public virtual IList AdultThemeInfos { get; set; }

查询所有UserID等于1的用户 发布的主题信息。Where中的条件语句须是表【Table="ThemeInfo"】中的字段名,不是属性名称。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值