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

来源:互联网 发布:lumia 813刷linux 编辑:程序博客网 时间:2024/05/17 05:05

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)
        }