ObjectQuery:ActiveRecord 强类型对象查询工具

ObjectQuery 是一款结合 ActiveRecord 的强类型对象查询工具,功能类似于 NHibernateQueryGenerator 但更为强大。支持多种查询方式如按条件查找、分组查询、聚合查询等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ObjectQuery是一个结合ActiveRecord的强类型对象查询工具,功能类似 Ayende的NHibernate Query Generator ,但更强大。
下载:ObjectQuery.src.rar  44KB
            ObjectQuery.bin.rar  611KB
使用示例:
None.gif Post[] findAll  =  Query.From(DB.Post).Where(DB.Post.Title.StartsWith( " Overloading " ||  DB.Post.Title  ==   " Operator " ).FindAll(); 
None.gif
object [][] groupby  =  Query.From(DB.Post).GroupBy(DB.Post.Title).FindAll(DB.Post.Title, Aggregate.Count(DB.Post));
None.gifDateTime
?  aggregate  =  Query.From(DB.Post).FindOne(Aggregate.Max(DB.Post.Created)); 
None.gifPost findFirst 
=  Query.From(DB.Post).OrderBy(DB.Post.Created.DescOrder()).FindFirst(); 
None.gifPost[] booltest 
=  Query.From(DB.Post).Where(DB.Post.Published).FindAll(); 
None.gifBlog[] listtest 
=  Query.From(DB.Blog).Where(DB.Blog.Posts.Count  >   0 ).FindAll(); 
None.gifPost[] math 
=  Query.From(DB.Post).Where(DB.Post.Id  +   1   >   0 ).FindAll(); 
None.gif

使用步骤:
一、通过数据库表定义或者直接写出ActiveRecord的类。
数据库表定义:

None.gif CREATE   TABLE   [ dbo ] . [ Blogs ]  (
None.gif    
[ blog_id ]   [ int ]   IDENTITY  ( 1 1 NOT   NULL  ,
None.gif    
[ blog_name ]   [ varchar ]  ( 50 NULL  ,
None.gif    
[ blog_author ]   [ varchar ]  ( 50 NULL  
None.gif
ON   [ PRIMARY ]
None.gif
None.gif
CREATE   TABLE   [ dbo ] . [ Posts ]  (
None.gif    
[ post_id ]   [ int ]   IDENTITY  ( 1 1 NOT   NULL  ,
None.gif    
[ post_title ]   [ varchar ]  ( 50 NULL  ,
None.gif    
[ post_contents ]   [ text ]   NULL  ,
None.gif    
[ post_category ]   [ varchar ]  ( 50 NULL  ,
None.gif    
[ post_blogid ]   [ int ]   NULL  ,
None.gif    
[ post_created ]   [ datetime ]   NULL  ,
None.gif    
[ post_published ]   [ bit ]   NULL  
None.gif
ON   [ PRIMARY ]  TEXTIMAGE_ON  [ PRIMARY ]
None.gif
ActiveRecord类定义:
ContractedBlock.gif ExpandedBlockStart.gif ActiveRecord类定义
  1None.gifnamespace ObjectQuery.Test
  2ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  3InBlock.gif    using System;
  4InBlock.gif    using System.Collections;
  5InBlock.gif    using System.Collections.Generic;
  6InBlock.gif    using Castle.ActiveRecord;
  7InBlock.gif    
  8InBlock.gif    
  9ContractedSubBlock.gifExpandedSubBlockStart.gif    class Blog#region class Blog
 10InBlock.gif    [ActiveRecord("Blogs")]
 11InBlock.gif    public partial class Blog : ActiveRecordBase<Blog>
 12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 13InBlock.gif        
 14InBlock.gif        private Int32 _id;
 15InBlock.gif        
 16InBlock.gif        private String _name;
 17InBlock.gif        
 18InBlock.gif        private String _author;
 19InBlock.gif        
 20InBlock.gif        private IList<Post> _posts = new List<Post>();
 21InBlock.gif        
 22InBlock.gif        [PrimaryKey(PrimaryKeyType.Native, "blog_id")]
 23InBlock.gif        public Int32 Id
 24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 25InBlock.gif            get
 26ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 27InBlock.gif                return this._id;
 28ExpandedSubBlockEnd.gif            }

 29InBlock.gif            set
 30ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 31InBlock.gif                this._id = value;
 32ExpandedSubBlockEnd.gif            }

 33ExpandedSubBlockEnd.gif        }

 34InBlock.gif        
 35InBlock.gif        [Property(Column="blog_name")]
 36InBlock.gif        public String Name
 37ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 38InBlock.gif            get
 39ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 40InBlock.gif                return this._name;
 41ExpandedSubBlockEnd.gif            }

 42InBlock.gif            set
 43ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 44InBlock.gif                this._name = value;
 45ExpandedSubBlockEnd.gif            }

 46ExpandedSubBlockEnd.gif        }

 47InBlock.gif        
 48InBlock.gif        [Property(Column="blog_author")]
 49InBlock.gif        public String Author
 50ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 51InBlock.gif            get
 52ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 53InBlock.gif                return this._author;
 54ExpandedSubBlockEnd.gif            }

 55InBlock.gif            set
 56ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 57InBlock.gif                this._author = value;
 58ExpandedSubBlockEnd.gif            }

 59ExpandedSubBlockEnd.gif        }

 60InBlock.gif        
 61InBlock.gif        [HasMany(Table="Posts", ColumnKey="post_blogid")]
 62InBlock.gif        public IList<Post> Posts
 63ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 64InBlock.gif            get
 65ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 66InBlock.gif                return this._posts;
 67ExpandedSubBlockEnd.gif            }

 68InBlock.gif            set
 69ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 70InBlock.gif                this._posts = value;
 71ExpandedSubBlockEnd.gif            }

 72ExpandedSubBlockEnd.gif        }

 73ExpandedSubBlockEnd.gif    }

 74ExpandedSubBlockEnd.gif    #endregion

 75InBlock.gif    
 76ContractedSubBlock.gifExpandedSubBlockStart.gif    class Post#region class Post
 77InBlock.gif    [ActiveRecord("Posts")]
 78InBlock.gif    public partial class Post : ActiveRecordBase<Post>
 79ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 80InBlock.gif        
 81InBlock.gif        private Int32 _id;
 82InBlock.gif        
 83InBlock.gif        private String _title;
 84InBlock.gif        
 85InBlock.gif        private String _contents;
 86InBlock.gif        
 87InBlock.gif        private String _category;
 88InBlock.gif        
 89InBlock.gif        private Nullable<System.DateTime> _created;
 90InBlock.gif        
 91InBlock.gif        private Nullable<bool> _published;
 92InBlock.gif        
 93InBlock.gif        private Blog _blog;
 94InBlock.gif        
 95InBlock.gif        [PrimaryKey(PrimaryKeyType.Native, "post_id")]
 96InBlock.gif        public Int32 Id
 97ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 98InBlock.gif            get
 99ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
100InBlock.gif                return this._id;
101ExpandedSubBlockEnd.gif            }

102InBlock.gif            set
103ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
104InBlock.gif                this._id = value;
105ExpandedSubBlockEnd.gif            }

106ExpandedSubBlockEnd.gif        }

107InBlock.gif        
108InBlock.gif        [Property(Column="post_title")]
109InBlock.gif        public String Title
110ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
111InBlock.gif            get
112ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
113InBlock.gif                return this._title;
114ExpandedSubBlockEnd.gif            }

115InBlock.gif            set
116ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
117InBlock.gif                this._title = value;
118ExpandedSubBlockEnd.gif            }

119ExpandedSubBlockEnd.gif        }

120InBlock.gif        
121InBlock.gif        [Property(Column="post_contents")]
122InBlock.gif        public String Contents
123ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
124InBlock.gif            get
125ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
126InBlock.gif                return this._contents;
127ExpandedSubBlockEnd.gif            }

128InBlock.gif            set
129ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
130InBlock.gif                this._contents = value;
131ExpandedSubBlockEnd.gif            }

132ExpandedSubBlockEnd.gif        }

133InBlock.gif        
134InBlock.gif        [Property(Column="post_category")]
135InBlock.gif        public String Category
136ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
137InBlock.gif            get
138ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
139InBlock.gif                return this._category;
140ExpandedSubBlockEnd.gif            }

141InBlock.gif            set
142ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
143InBlock.gif                this._category = value;
144ExpandedSubBlockEnd.gif            }

145ExpandedSubBlockEnd.gif        }

146InBlock.gif        
147InBlock.gif        [Property(Column="post_created")]
148InBlock.gif        public Nullable<System.DateTime> Created
149ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
150InBlock.gif            get
151ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
152InBlock.gif                return this._created;
153ExpandedSubBlockEnd.gif            }

154InBlock.gif            set
155ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
156InBlock.gif                this._created = value;
157ExpandedSubBlockEnd.gif            }

158ExpandedSubBlockEnd.gif        }

159InBlock.gif        
160InBlock.gif        [Property(Column="post_published")]
161InBlock.gif        public Nullable<bool> Published
162ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
163InBlock.gif            get
164ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
165InBlock.gif                return this._published;
166ExpandedSubBlockEnd.gif            }

167InBlock.gif            set
168ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
169InBlock.gif                this._published = value;
170ExpandedSubBlockEnd.gif            }

171ExpandedSubBlockEnd.gif        }

172InBlock.gif        
173InBlock.gif        [BelongsTo("post_blogid")]
174InBlock.gif        public Blog Blog
175ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
176InBlock.gif            get
177ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
178InBlock.gif                return this._blog;
179ExpandedSubBlockEnd.gif            }

180InBlock.gif            set
181ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
182InBlock.gif                this._blog = value;
183ExpandedSubBlockEnd.gif            }

184ExpandedSubBlockEnd.gif        }

185ExpandedSubBlockEnd.gif    }

186ExpandedSubBlockEnd.gif    #endregion

187ExpandedBlockEnd.gif}

188None.gif

二、将ActiveRecord类加入一个项目(ObjectQuery.Test),编译项目生成dll或者exe文件(ObjectQuery.Test.exe)。
三、通过命令行运行代码生成工具ObjectQuery.Generator.exe,生成ObjectQuery.gen.cs / .vb 文件。
        ObjectQuery.Generator.exe cs e:\temp\ObjectQuery.Test.exe e:\temp
ContractedBlock.gif ExpandedBlockStart.gif 代码生成器生成的代码
  1None.gif//------------------------------------------------------------------------------
  2None.gif// <auto-generated>
  3None.gif//     此代码由工具生成。
  4None.gif//     运行库版本:2.0.50727.42
  5None.gif//
  6None.gif//     对此文件的更改可能会导致不正确的行为,并且如果
  7None.gif//     重新生成代码,这些更改将会丢失。
  8None.gif// </auto-generated>
  9None.gif//------------------------------------------------------------------------------
 10None.gif
 11None.gifnamespace ObjectQuery.Test
 12ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 13InBlock.gif    using System;
 14InBlock.gif    using System.Collections;
 15InBlock.gif    using System.Collections.Generic;
 16InBlock.gif    using ObjectQuery;
 17InBlock.gif    using ObjectQuery.Property;
 18InBlock.gif    
 19InBlock.gif    
 20InBlock.gif    public class DB
 21ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 22InBlock.gif        
 23InBlock.gif        public static Criteria.BlogCriteria Blog = new Criteria.BlogCriteria();
 24InBlock.gif        
 25InBlock.gif        public static Criteria.PostCriteria Post = new Criteria.PostCriteria();
 26ExpandedSubBlockEnd.gif    }

 27InBlock.gif    
 28InBlock.gif    public class Criteria
 29ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 30InBlock.gif        
 31ContractedSubBlock.gifExpandedSubBlockStart.gif        class BlogCriteria#region class BlogCriteria
 32InBlock.gif        public class BlogCriteria : AbstractCriteria<ObjectQuery.Test.Blog>
 33ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 34InBlock.gif            
 35InBlock.gif            private ValueTypeProperty<int> _Id;
 36InBlock.gif            
 37InBlock.gif            private StringProperty _Name;
 38InBlock.gif            
 39InBlock.gif            private StringProperty _Author;
 40InBlock.gif            
 41InBlock.gif            private ListProperty _Posts;
 42InBlock.gif            
 43InBlock.gif            public BlogCriteria()
 44ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 45ExpandedSubBlockEnd.gif            }

 46InBlock.gif            
 47InBlock.gif            internal BlogCriteria(string path) : 
 48InBlock.gif                    base(path)
 49ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 50ExpandedSubBlockEnd.gif            }

 51InBlock.gif            
 52InBlock.gif            public ValueTypeProperty<int> Id
 53ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 54InBlock.gif                get
 55ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 56InBlock.gif                    if ((((object)(_Id)) == null))
 57ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 58InBlock.gif                        _Id = new ValueTypeProperty<int>((_path + ".Id"));
 59ExpandedSubBlockEnd.gif                    }

 60InBlock.gif                    return _Id;
 61ExpandedSubBlockEnd.gif                }

 62ExpandedSubBlockEnd.gif            }

 63InBlock.gif            
 64InBlock.gif            public StringProperty Name
 65ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 66InBlock.gif                get
 67ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 68InBlock.gif                    if ((((object)(_Name)) == null))
 69ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 70InBlock.gif                        _Name = new StringProperty((_path + ".Name"));
 71ExpandedSubBlockEnd.gif                    }

 72InBlock.gif                    return _Name;
 73ExpandedSubBlockEnd.gif                }

 74ExpandedSubBlockEnd.gif            }

 75InBlock.gif            
 76InBlock.gif            public StringProperty Author
 77ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 78InBlock.gif                get
 79ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 80InBlock.gif                    if ((((object)(_Author)) == null))
 81ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 82InBlock.gif                        _Author = new StringProperty((_path + ".Author"));
 83ExpandedSubBlockEnd.gif                    }

 84InBlock.gif                    return _Author;
 85ExpandedSubBlockEnd.gif                }

 86ExpandedSubBlockEnd.gif            }

 87InBlock.gif            
 88InBlock.gif            public ListProperty Posts
 89ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 90InBlock.gif                get
 91ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 92InBlock.gif                    if ((((object)(_Posts)) == null))
 93ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 94InBlock.gif                        _Posts = new ListProperty((_path + ".Posts"));
 95ExpandedSubBlockEnd.gif                    }

 96InBlock.gif                    return _Posts;
 97ExpandedSubBlockEnd.gif                }

 98ExpandedSubBlockEnd.gif            }

 99ExpandedSubBlockEnd.gif        }

100ExpandedSubBlockEnd.gif        #endregion

101InBlock.gif        
102ContractedSubBlock.gifExpandedSubBlockStart.gif        class PostCriteria#region class PostCriteria
103InBlock.gif        public class PostCriteria : AbstractCriteria<ObjectQuery.Test.Post>
104ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
105InBlock.gif            
106InBlock.gif            private ValueTypeProperty<int> _Id;
107InBlock.gif            
108InBlock.gif            private StringProperty _Title;
109InBlock.gif            
110InBlock.gif            private StringProperty _Contents;
111InBlock.gif            
112InBlock.gif            private StringProperty _Category;
113InBlock.gif            
114InBlock.gif            private ValueTypeProperty<System.Nullable<System.DateTime>> _Created;
115InBlock.gif            
116InBlock.gif            private BooleanProperty _Published;
117InBlock.gif            
118InBlock.gif            private BlogCriteria _Blog;
119InBlock.gif            
120InBlock.gif            public PostCriteria()
121ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
122ExpandedSubBlockEnd.gif            }

123InBlock.gif            
124InBlock.gif            internal PostCriteria(string path) : 
125InBlock.gif                    base(path)
126ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
127ExpandedSubBlockEnd.gif            }

128InBlock.gif            
129InBlock.gif            public ValueTypeProperty<int> Id
130ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
131InBlock.gif                get
132ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
133InBlock.gif                    if ((((object)(_Id)) == null))
134ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
135InBlock.gif                        _Id = new ValueTypeProperty<int>((_path + ".Id"));
136ExpandedSubBlockEnd.gif                    }

137InBlock.gif                    return _Id;
138ExpandedSubBlockEnd.gif                }

139ExpandedSubBlockEnd.gif            }

140InBlock.gif            
141InBlock.gif            public StringProperty Title
142ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
143InBlock.gif                get
144ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
145InBlock.gif                    if ((((object)(_Title)) == null))
146ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
147InBlock.gif                        _Title = new StringProperty((_path + ".Title"));
148ExpandedSubBlockEnd.gif                    }

149InBlock.gif                    return _Title;
150ExpandedSubBlockEnd.gif                }

151ExpandedSubBlockEnd.gif            }

152InBlock.gif            
153InBlock.gif            public StringProperty Contents
154ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
155InBlock.gif                get
156ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
157InBlock.gif                    if ((((object)(_Contents)) == null))
158ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
159InBlock.gif                        _Contents = new StringProperty((_path + ".Contents"));
160ExpandedSubBlockEnd.gif                    }

161InBlock.gif                    return _Contents;
162ExpandedSubBlockEnd.gif                }

163ExpandedSubBlockEnd.gif            }

164InBlock.gif            
165InBlock.gif            public StringProperty Category
166ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
167InBlock.gif                get
168ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
169InBlock.gif                    if ((((object)(_Category)) == null))
170ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
171InBlock.gif                        _Category = new StringProperty((_path + ".Category"));
172ExpandedSubBlockEnd.gif                    }

173InBlock.gif                    return _Category;
174ExpandedSubBlockEnd.gif                }

175ExpandedSubBlockEnd.gif            }

176InBlock.gif            
177InBlock.gif            public ValueTypeProperty<System.Nullable<System.DateTime>> Created
178ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
179InBlock.gif                get
180ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
181InBlock.gif                    if ((((object)(_Created)) == null))
182ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
183InBlock.gif                        _Created = new ValueTypeProperty<System.Nullable<System.DateTime>>((_path + ".Created"));
184ExpandedSubBlockEnd.gif                    }

185InBlock.gif                    return _Created;
186ExpandedSubBlockEnd.gif                }

187ExpandedSubBlockEnd.gif            }

188InBlock.gif            
189InBlock.gif            public BooleanProperty Published
190ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
191InBlock.gif                get
192ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
193InBlock.gif                    if ((((object)(_Published)) == null))
194ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
195InBlock.gif                        _Published = new BooleanProperty((_path + ".Published"));
196ExpandedSubBlockEnd.gif                    }

197InBlock.gif                    return _Published;
198ExpandedSubBlockEnd.gif                }

199ExpandedSubBlockEnd.gif            }

200InBlock.gif            
201InBlock.gif            public BlogCriteria Blog
202ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
203InBlock.gif                get
204ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
205InBlock.gif                    if ((((object)(_Blog)) == null))
206ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
207InBlock.gif                        _Blog = new BlogCriteria((_path + ".Blog"));
208ExpandedSubBlockEnd.gif                    }

209InBlock.gif                    return _Blog;
210ExpandedSubBlockEnd.gif                }

211ExpandedSubBlockEnd.gif            }

212ExpandedSubBlockEnd.gif        }

213ExpandedSubBlockEnd.gif        #endregion

214ExpandedSubBlockEnd.gif    }

215ExpandedBlockEnd.gif}

216None.gif

四、将代码生成器生成的文件加入到项目中,就可以使用ObjectQuery的强大功能了。
ContractedBlock.gif ExpandedBlockStart.gif 测试代码
 1None.gif            Post.DeleteAll();
 2None.gif            Blog.DeleteAll();
 3None.gif
 4None.gif            Blog blog = new Blog();
 5None.gif            blog.Name = "tinifish @ blog";
 6None.gif            blog.Author = "tinifish";
 7None.gif            blog.Save();
 8None.gif
 9None.gif            Blog blog1 = new Blog();
10None.gif            blog1.Name = "tinifish @ blog";
11None.gif            blog1.Author = "tinifish";
12None.gif            blog1.Save();
13None.gif
14None.gif            Post post = new Post();
15None.gif            post.Blog = blog;
16None.gif            post.Title = "I love Active Record";
17None.gif            post.Contents = "Indeed I do!";
18None.gif            post.Category = "dotnet";
19None.gif            post.Created = DateTime.Now;
20None.gif            post.Published = false;
21None.gif            post.Save();
22None.gif
23None.gif            Post post1 = new Post();
24None.gif            //post1.Blog = blog;
25None.gif            post1.Title = "Overloading post";
26None.gif            post1.Contents = "Indeed I do!";
27None.gif            post1.Category = "DOTNET";
28None.gif            post1.Created = DateTime.Now;
29None.gif            post1.Published = true;
30None.gif            post1.Save();
31None.gif
32None.gif            using (new SessionScope())
33ExpandedBlockStart.gifContractedBlock.gif            dot.gif{
34InBlock.gif                Post[] findAll = Query.From(DB.Post).Where(DB.Post.Title.StartsWith("Overloading"|| DB.Post.Title == "Operator").FindAll();
35InBlock.gif                Assert.IsTrue(findAll.Length == 1 && findAll[0].Id == post1.Id);
36InBlock.gif
37InBlock.gif                Post[] findAll1 = Query.From(DB.Post).Where(DB.Post.Blog == blog
38InBlock.gif                    && DB.Post.Title.StartsWith("Overloading"|| DB.Post.Title == "Operator").FindAll();
39InBlock.gif                Assert.IsTrue(findAll1.Length == 0);
40InBlock.gif
41InBlock.gif                object[][] groupby = Query.From(DB.Post).GroupBy(DB.Post.Title).FindAll(DB.Post.Title, Aggregate.Count(DB.Post));
42InBlock.gif                Assert.IsTrue(groupby.Length == 2);
43InBlock.gif
44InBlock.gif                DateTime? aggregate = Query.From(DB.Post).FindOne(Aggregate.Max(DB.Post.Created));
45InBlock.gif                Post findFirst = Query.From(DB.Post).OrderBy(DB.Post.Created.DescOrder()).FindFirst();
46InBlock.gif                Assert.AreEqual(aggregate, findFirst.Created);
47InBlock.gif
48InBlock.gif                Post[] booltest = Query.From(DB.Post).Where(DB.Post.Published).FindAll();
49InBlock.gif                Assert.IsTrue(booltest.Length == 1 && booltest[0].Id == post1.Id);
50InBlock.gif
51InBlock.gif                Blog[] listtest = Query.From(DB.Blog).Where(DB.Blog.Posts.Count > 0).FindAll();
52InBlock.gif                Assert.IsTrue(listtest.Length == 1 && listtest[0].Id == blog.Id);
53InBlock.gif
54InBlock.gif                Post[] math = Query.From(DB.Post).Where(DB.Post.Id + 1 > 0).FindAll();
55InBlock.gif

项目所使用的NHibernate和Castle.ActiveRecord的版本为svn服务器上的最新版本。
 这是我在博客园发的第一篇随笔,请大家多多支持。欢迎功能建议和意见反馈。

转载于:https://www.cnblogs.com/benfish/archive/2006/10/16/530269.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值