您的位置:首页 > 其它

Castle 中ActiveRecord查询部分字段属性的投影(Projections)使用教程

2009-04-27 16:27 423 查看
Castle 中ActiveRecord查询部分字段属性的投影(Projections)使用教程



很多 Castle ActiveRecord系列教程都没有介绍投影(Projections)的使用,这里我用代码举例下....



查询部分字段属性..很方便灵活...同时效率也提高很不少..特别是大表多字段的情况下...(问题咨询QQ1163551688)



[Test]

public void ScalarProjectionQueryTest()

{

Blog blog = new Blog();

blog.Name = "hammett's blog";

blog.Author = "hamilton verissimo";

blog.Save();

ScalarProjectionQuery<Blog, int> proj = new ScalarProjectionQuery<Blog, int>(Projections.RowCount());

int rowCount = proj.Execute();

Assert.AreEqual(1, rowCount);.(问题咨询QQ1163551688)

}

[Test]

public void UnTypedProjectionQueryTest()

{

Blog blog = new Blog();

blog.Name = "hammett's blog";

blog.Author = "hamilton verissimo";

blog.Save();

ProjectionQuery<Blog> proj = new ProjectionQuery<Blog>(

Projections.ProjectionList()

.Add(Projections.Property("Name"))

.Add(Projections.Property("Author")));

IList<object[]> results = proj.Execute();

Assert.AreEqual(blog.Name, results[0][0]);

Assert.AreEqual(blog.Author, results[0][1]);.(问题咨询QQ1163551688)

}

[Test]

public void TypedProjectionQueryTest()

{

Blog blog = new Blog();

blog.Name = "hammett's blog";

blog.Author = "hamilton verissimo";

blog.Save();

ProjectionQuery<Blog, KeyValuePair<string, string>> proj = new ProjectionQuery<Blog, KeyValuePair<string, string>>(

Projections.ProjectionList()

.Add(Projections.Property("Name"))

.Add(Projections.Property("Author")));

IList<KeyValuePair<string, string>> results = proj.Execute();

Assert.AreEqual(blog.Name, results[0].Key);

Assert.AreEqual(blog.Author, results[0].Value);.(问题咨询QQ1163551688)

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: