当前位置: 首页>>代码示例>>C#>>正文


C# Criteria.Student类代码示例

本文整理汇总了C#中NHibernate.Test.Criteria.Student的典型用法代码示例。如果您正苦于以下问题:C# Student类的具体用法?C# Student怎么用?C# Student使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


Student类属于NHibernate.Test.Criteria命名空间,在下文中一共展示了Student类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: OnSetUp

		protected override void OnSetUp()
		{
			using (ISession session = OpenSession())
			{
				ITransaction t = session.BeginTransaction();

				Student gavin = new Student();
				gavin.Name = "ayende";
				gavin.StudentNumber = 27;
				session.Save(gavin);

				t.Commit();
			}
		}
开发者ID:marchlud,项目名称:nhibernate-core,代码行数:14,代码来源:ProjectionsTest.cs

示例2: LikeProjectionTest

		public void LikeProjectionTest()
		{
			Student john = new Student { Name = "John" };
			using (ISession session = this.OpenSession())
			{
				session.Save(john);
				session.Flush();
			}

			using (ISession session = this.OpenSession())
			{
				ICriteria criteria = session.CreateCriteria(typeof(Student), "c");

				criteria.Add(new LikeExpression(Projections.Property("Name"), "John", MatchMode.Anywhere));

				Assert.AreEqual(1, criteria.List().Count);
			}

			using (ISession session = this.OpenSession())
			{
				ICriteria criteria = session.CreateCriteria(typeof(Student), "c");

				criteria.Add(new LikeExpression("Name", "John"));

				Assert.AreEqual(1, criteria.List().Count);
			}

			using (ISession session = this.OpenSession())
			{
				session.Delete(john);
				session.Flush();
			}
		}
开发者ID:NikGovorov,项目名称:nhibernate-core,代码行数:33,代码来源:CriteriaQueryTest.cs

示例3: OrderProjectionAliasedTest

		public void OrderProjectionAliasedTest()
		{
			using (ISession session = OpenSession())
			using (ITransaction t = session.BeginTransaction())
			{

				Course courseA = new Course();
				courseA.CourseCode = "HIB-A";
				courseA.Description = "Hibernate Training A";
				session.Save(courseA);
			
				Student gavin = new Student();
				gavin.Name = "Gavin King";
				gavin.StudentNumber = 232;
				gavin.PreferredCourse = courseA;
				session.Save(gavin);
			
				Student leonardo = new Student();
				leonardo.Name = "Leonardo Quijano";
				leonardo.StudentNumber = 233;
				leonardo.PreferredCourse = courseA;
				session.Save(leonardo);
			
				Student johnDoe = new Student();
				johnDoe.Name = "John Doe";
				johnDoe.StudentNumber = 235;
				johnDoe.PreferredCourse = null;
				session.Save(johnDoe);
			
				IProjection conditional =
					Projections.Conditional(
						Restrictions.Eq("Name", "Gavin King"),
						Projections.Constant("Name"),
						Projections.Constant("AnotherName"));

				ICriteria criteria = session.CreateCriteria(typeof(Student));
				criteria.SetMaxResults(1);
				criteria.SetFirstResult(1);
				IList result = criteria.SetProjection(Projections.Alias(conditional, "CheckName"))
					.AddOrder(Order.Asc("CheckName"))
					.List();
	
				session.Delete(gavin);
				session.Delete(leonardo);
				session.Delete(johnDoe);
				session.Delete(courseA);

				t.Commit();
			}
		}
开发者ID:NikGovorov,项目名称:nhibernate-core,代码行数:50,代码来源:CriteriaQueryTest.cs

示例4: SqlExpressionWithParameters

		public void SqlExpressionWithParameters()
		{
			using (ISession session = OpenSession())
			using (ITransaction t = session.BeginTransaction())
			{
				ICriteria c = session.CreateCriteria(typeof(Student));
				c.Add(Expression.Eq("StudentNumber", (long)232));
				c.Add(Expression.Sql("2 = ?", 1, NHibernateUtil.Int32));

				Student gavin = new Student();
				gavin.Name = "Gavin King";
				gavin.StudentNumber = 232;
				session.Save(gavin);

				IList result = c.List();

				Assert.AreEqual(0, result.Count);

				session.Delete(gavin);

				t.Commit();
			}
		}
开发者ID:NikGovorov,项目名称:nhibernate-core,代码行数:23,代码来源:CriteriaQueryTest.cs

示例5: SameColumnAndAliasNamesResultTransformer

		public void SameColumnAndAliasNamesResultTransformer()
		{
			DetachedCriteria dc = DetachedCriteria.For(typeof(Student))
				.SetProjection(
					Projections.ProjectionList()
						.Add(Projections.Property("StudentNumber"), "StudentNumber")
						.Add(Projections.Property("Name"), "Name"))
				.SetResultTransformer(new AliasToBeanResultTransformer(typeof(Student)))
				.Add(Property.ForName("Name").Eq("Gavin King"))
				.AddOrder(Order.Asc("StudentNumber"));


			ISession session = OpenSession();
			ITransaction t = session.BeginTransaction();

			Student gavin = new Student();
			gavin.Name = "Gavin King";
			gavin.StudentNumber = 232;
			Student bizarroGavin = new Student();
			bizarroGavin.Name = "Gavin King";
			bizarroGavin.StudentNumber = 666;
			session.Save(bizarroGavin);
			session.Save(gavin);

			IList result = dc.GetExecutableCriteria(session)
				.SetMaxResults(3)
				.List();

			Assert.That(result.Count, Is.EqualTo(2));
			Assert.That(result[0], Is.InstanceOf(typeof(Student)));
			Assert.That(result[1], Is.InstanceOf(typeof(Student)));

			session.Delete(gavin);
			session.Delete(bizarroGavin);
			t.Commit();
			session.Close();
		}
开发者ID:NikGovorov,项目名称:nhibernate-core,代码行数:37,代码来源:CriteriaQueryTest.cs

示例6: ProjectionsUsingProperty

		public void ProjectionsUsingProperty()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();

			Course course = new Course();
			course.CourseCode = "HIB";
			course.Description = "Hibernate Training";
			course.CourseMeetings.Add(new CourseMeeting(course, "Monday", 1, "1313 Mockingbird Lane"));
			s.Save(course);

			Student gavin = new Student();
			gavin.Name = "Gavin King";
			gavin.StudentNumber = 667;
			CityState odessaWa = new CityState("Odessa", "WA");
			gavin.CityState = odessaWa;
			gavin.PreferredCourse = course;
			s.Save(gavin);

			Student xam = new Student();
			xam.Name = "Max Rydahl Andersen";
			xam.StudentNumber = 101;
			s.Save(xam);

			Enrolment enrolment = new Enrolment();
			enrolment.Course = course;
			enrolment.CourseCode = course.CourseCode;
			enrolment.Semester = 1;
			enrolment.Year = 1999;
			enrolment.Student = xam;
			enrolment.StudentNumber = xam.StudentNumber;
			xam.Enrolments.Add(enrolment);
			s.Save(enrolment);

			enrolment = new Enrolment();
			enrolment.Course = course;
			enrolment.CourseCode = course.CourseCode;
			enrolment.Semester = 3;
			enrolment.Year = 1998;
			enrolment.Student = gavin;
			enrolment.StudentNumber = gavin.StudentNumber;
			gavin.Enrolments.Add(enrolment);
			s.Save(enrolment);
			s.Flush();

			// Subtest #1
			IList resultList = s.CreateCriteria<Enrolment>()
				.SetProjection(Projections.ProjectionList()
					.Add(Property.ForName("Student"), "student")
					.Add(Property.ForName("Course"), "course")
					.Add(Property.ForName("Semester"), "semester")
					.Add(Property.ForName("Year"), "year")
				).List();

			Assert.That(resultList.Count, Is.EqualTo(2));
			
			foreach (object[] objects in resultList)
			{
				Assert.That(objects.Length, Is.EqualTo(4));
				Assert.That(objects[0], Is.InstanceOf<Student>());
				Assert.That(objects[1], Is.InstanceOf<Course>());
				Assert.That(objects[2], Is.InstanceOf<short>());
				Assert.That(objects[3], Is.InstanceOf<short>());
			}

			// Subtest #2
			resultList = s.CreateCriteria<Student>()
				.SetProjection(Projections.ProjectionList()
					.Add(Projections.Id().As("StudentNumber"))
					.Add(Property.ForName("Name"), "name")
					.Add(Property.ForName("CityState"), "cityState")
					.Add(Property.ForName("PreferredCourse"), "preferredCourse")
				).List();
			
			Assert.That(resultList.Count, Is.EqualTo(2));
			
			foreach(object[] objects in resultList)
			{
				Assert.That(objects.Length, Is.EqualTo(4));
				Assert.That(objects[0], Is.InstanceOf<long>());
				Assert.That(objects[1], Is.InstanceOf<string>());
				
				if ("Gavin King".Equals(objects[1]))
				{
					Assert.That(objects[2], Is.InstanceOf<CityState>());
					Assert.That(objects[3], Is.InstanceOf<Course>());
				}
				else
				{
					Assert.That(objects[2], Is.Null);
					Assert.That(objects[3], Is.Null);
				}
			}
			
			// Subtest #3
			resultList = s.CreateCriteria<Student>()
				.Add(Restrictions.Eq("Name", "Gavin King"))
				.SetProjection(Projections.ProjectionList()
					.Add(Projections.Id().As("StudentNumber"))
					.Add(Property.ForName("Name"), "name")
//.........这里部分代码省略.........
开发者ID:NikGovorov,项目名称:nhibernate-core,代码行数:101,代码来源:CriteriaQueryTest.cs

示例7: NH_1155_ShouldNotLoadAllChildrenInPagedSubSelect

		public void NH_1155_ShouldNotLoadAllChildrenInPagedSubSelect()
		{
			if (this.Dialect.GetType().Equals((typeof(MsSql2000Dialect))))
				Assert.Ignore("This is not fixed for SQL 2000 Dialect");

			using (ISession s = OpenSession())
			using (ITransaction tx = s.BeginTransaction())
			{
				Course course = new Course();
				course.CourseCode = "HIB";
				course.Description = "Hibernate Training";
				s.Save(course);


				Student gavin = new Student();
				gavin.Name = "Gavin King";
				gavin.StudentNumber = 667;
				s.Save(gavin);

				Student ayende = new Student();
				ayende.Name = "Ayende Rahien";
				ayende.StudentNumber = 1337;
				s.Save(ayende);


				Student xam = new Student();
				xam.Name = "Max Rydahl Andersen";
				xam.StudentNumber = 101;
				s.Save(xam);

				Enrolment enrolment = new Enrolment();
				enrolment.Course = course;
				enrolment.CourseCode = course.CourseCode;
				enrolment.Semester = 1;
				enrolment.Year = 1999;
				enrolment.Student = xam;
				enrolment.StudentNumber = xam.StudentNumber;
				xam.Enrolments.Add(enrolment);
				s.Save(enrolment);

				enrolment = new Enrolment();
				enrolment.Course = course;
				enrolment.CourseCode = course.CourseCode;
				enrolment.Semester = 3;
				enrolment.Year = 1998;
				enrolment.Student = ayende;
				enrolment.StudentNumber = ayende.StudentNumber;
				ayende.Enrolments.Add(enrolment);
				s.Save(enrolment);
				tx.Commit();
			}

			using (ISession s = OpenSession())
			{
				IList<Student> list = s.CreateCriteria(typeof(Student))
					.SetFirstResult(1)
					.SetMaxResults(10)
					.AddOrder(Order.Asc("StudentNumber"))
					.List<Student>();
				foreach (Student student in list)
				{
					foreach (Enrolment enrolment in student.Enrolments)
					{
						NHibernateUtil.Initialize(enrolment);
					}
				}

				Enrolment key = new Enrolment();
				key.CourseCode = "HIB";
				key.StudentNumber = 101;// xam
				//since we didn't load xam's entrollments before (skipped by orderring)
				//it should not be already loaded
				Enrolment shouldNotBeLoaded = (Enrolment)s.Load(typeof(Enrolment), key);
				Assert.IsFalse(NHibernateUtil.IsInitialized(shouldNotBeLoaded));
			}

			using (ISession s = OpenSession())
			using (ITransaction tx = s.BeginTransaction())
			{
				s.Delete("from Enrolment");
				s.Delete("from Student");
				s.Delete("from Course");
				tx.Commit();
			}
		}
开发者ID:NikGovorov,项目名称:nhibernate-core,代码行数:85,代码来源:CriteriaQueryTest.cs

示例8: CloningDetachedCriteriaTest

		public void CloningDetachedCriteriaTest()
		{
			DetachedCriteria dc = DetachedCriteria.For(typeof(Student))
				.Add(Property.ForName("Name").Eq("Gavin King"))
				.SetProjection(Property.ForName("StudentNumber"));

			ISession session = OpenSession();
			ITransaction t = session.BeginTransaction();

			Student gavin = new Student();
			gavin.Name = "Gavin King";
			gavin.StudentNumber = 232;
			Student bizarroGavin = new Student();
			bizarroGavin.Name = "Gavin King";
			bizarroGavin.StudentNumber = 666;
			session.Save(bizarroGavin);
			session.Save(gavin);

			IList result = CriteriaTransformer.Clone(dc)
				.AddOrder(Order.Asc("StudentNumber"))
				.GetExecutableCriteria(session)
				.SetMaxResults(3)
				.List();

			Assert.AreEqual(2, result.Count);
			Assert.AreEqual(232L, result[0]);
			Assert.AreEqual(666L, result[1]);

			int count = (int)CriteriaTransformer.Clone(dc)
								.SetProjection(Projections.RowCount())
								.GetExecutableCriteria(session)
								.UniqueResult();
			Assert.AreEqual(2, count);

			session.Delete(gavin);
			session.Delete(bizarroGavin);
			t.Commit();
			session.Close();
		}
开发者ID:NikGovorov,项目名称:nhibernate-core,代码行数:39,代码来源:CriteriaQueryTest.cs

示例9: ProjectedListIncludesComponent

		public void ProjectedListIncludesComponent()
		{
			using (ISession s = OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				Student gaith = new Student();
				gaith.Name = "Gaith Bell";
				gaith.StudentNumber = 123;
				gaith.CityState = new CityState("Chicago", "Illinois");
				s.Save(gaith);
				s.Flush();
	
				IList data = (IList)s.CreateCriteria<Student>()
					.SetProjection(Projections.ProjectionList()
						.Add(Projections.Property("CityState"))
						.Add(Projections.Property("Name")))
						.List();
				
				t.Rollback();
			}
		}
开发者ID:NikGovorov,项目名称:nhibernate-core,代码行数:21,代码来源:CriteriaQueryTest.cs

示例10: ProjectedListIncludesEmbeddedCompositeId

		public void ProjectedListIncludesEmbeddedCompositeId()
		{
			using (ISession s = OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				Course course = new Course();
				course.CourseCode = "HIB";
				course.Description = "Hibernate Training";
				s.Save(course);
				
				Student gavin = new Student();
				gavin.Name = "Gavin King";
				gavin.StudentNumber = 667;
				s.Save(gavin);
				
				Student xam = new Student();
				xam.Name = "Max Rydahl Andersen";
				xam.StudentNumber = 101;
				s.Save(xam);
	
				Enrolment enrolment = new Enrolment();
				enrolment.Course = course;
				enrolment.CourseCode = course.CourseCode;
				enrolment.Semester = 1;
				enrolment.Year = 1999;
				enrolment.Student = xam;
				enrolment.StudentNumber = xam.StudentNumber;
				gavin.Enrolments.Add(enrolment);
				s.Save(enrolment);
				
				enrolment = new Enrolment();
				enrolment.Course = course;
				enrolment.CourseCode = course.CourseCode;
				enrolment.Semester = 3;
				enrolment.Year = 1998;
				enrolment.Student = gavin;
				enrolment.StudentNumber = gavin.StudentNumber;
				gavin.Enrolments.Add(enrolment);
				s.Save(enrolment);
	
				s.Flush();
				
				IList data = (IList)s.CreateCriteria<Enrolment>()
					.SetProjection(Projections.ProjectionList()
						.Add(Projections.Property("Semester"))
						.Add(Projections.Property("Year"))
						.Add(Projections.Id()))
				.List();
				
				t.Rollback();
			}
		}
开发者ID:NikGovorov,项目名称:nhibernate-core,代码行数:52,代码来源:CriteriaQueryTest.cs

示例11: CloningProjectionsUsingProperty

		public void CloningProjectionsUsingProperty()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();

			Course course = new Course();
			course.CourseCode = "HIB";
			course.Description = "Hibernate Training";
			s.Save(course);

			Student gavin = new Student();
			gavin.Name = "Gavin King";
			gavin.StudentNumber = 667;
			s.Save(gavin);

			Student xam = new Student();
			xam.Name = "Max Rydahl Andersen";
			xam.StudentNumber = 101;
			s.Save(xam);

			Enrolment enrolment = new Enrolment();
			enrolment.Course = course;
			enrolment.CourseCode = course.CourseCode;
			enrolment.Semester = 1;
			enrolment.Year = 1999;
			enrolment.Student = xam;
			enrolment.StudentNumber = xam.StudentNumber;
			xam.Enrolments.Add(enrolment);
			s.Save(enrolment);

			enrolment = new Enrolment();
			enrolment.Course = course;
			enrolment.CourseCode = course.CourseCode;
			enrolment.Semester = 3;
			enrolment.Year = 1998;
			enrolment.Student = gavin;
			enrolment.StudentNumber = gavin.StudentNumber;
			gavin.Enrolments.Add(enrolment);
			s.Save(enrolment);
			s.Flush();

			int count = (int)CriteriaTransformer.Clone(s.CreateCriteria(typeof(Enrolment))
															.SetProjection(Property.ForName("StudentNumber").Count().SetDistinct())
								)
								.UniqueResult();
			Assert.AreEqual(2, count);

			object obj = CriteriaTransformer.Clone(s.CreateCriteria(typeof(Enrolment))
													.SetProjection(Projections.ProjectionList()
																	.Add(Property.ForName("StudentNumber").Count())
																	.Add(Property.ForName("StudentNumber").Max())
																	.Add(Property.ForName("StudentNumber").Min())
																	.Add(Property.ForName("StudentNumber").Avg())
													)
				)
				.UniqueResult();
			object[] result = (object[])obj;

			Assert.AreEqual(2, result[0]);
			Assert.AreEqual(667L, result[1]);
			Assert.AreEqual(101L, result[2]);
			Assert.AreEqual(384.0D, (double)result[3], 0.01D);


			CriteriaTransformer.Clone(
				s.CreateCriteria(typeof(Enrolment))
					.Add(Property.ForName("StudentNumber").Gt(665L))
					.Add(Property.ForName("StudentNumber").Lt(668L))
					.Add(Property.ForName("CourseCode").Like("HIB", MatchMode.Start))
					.Add(Property.ForName("Year").Eq((short)1999))
					.AddOrder(Property.ForName("StudentNumber").Asc())
				)
				.UniqueResult();

			ICriteria clonedCriteriaProjection = CriteriaTransformer.Clone(s.CreateCriteria(typeof(Enrolment))
																			.SetProjection(Projections.ProjectionList()
																							.Add(Property.ForName("StudentNumber").As("stNumber"))
																							.Add(Property.ForName("CourseCode").As("cCode"))
																			)
																			.Add(Property.ForName("StudentNumber").Gt(665L))
																			.Add(Property.ForName("StudentNumber").Lt(668L))
																			.AddOrder(Property.ForName("StudentNumber").Asc())
																			.SetResultTransformer(CriteriaSpecification.AliasToEntityMap)
				);
			IList resultWithMaps = clonedCriteriaProjection
				.List();

			Assert.AreEqual(1, resultWithMaps.Count);
			IDictionary m1 = (IDictionary)resultWithMaps[0];

			Assert.AreEqual(667L, m1["stNumber"]);
			Assert.AreEqual(course.CourseCode, m1["cCode"]);

			resultWithMaps = CriteriaTransformer.Clone(s.CreateCriteria(typeof(Enrolment))
														.SetProjection(Property.ForName("StudentNumber").As("stNumber"))
														.AddOrder(Order.Desc("stNumber"))
														.SetResultTransformer(CriteriaSpecification.AliasToEntityMap)
				)
				.List();

//.........这里部分代码省略.........
开发者ID:NikGovorov,项目名称:nhibernate-core,代码行数:101,代码来源:CriteriaQueryTest.cs

示例12: GroupByComponent

		public void GroupByComponent()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();

			Course course = new Course();
			course.CourseCode = "HIB";
			course.Description = "Hibernate Training";
			s.Save(course);

			Student gavin = new Student();
			gavin.Name = "Gavin King";
			gavin.StudentNumber = 667;
			gavin.CityState = new CityState("Odessa", "WA");;
			s.Save(gavin);

			Student xam = new Student();
			xam.Name = "Max Rydahl Andersen";
			xam.StudentNumber = 101;
			xam.PreferredCourse = course;
			xam.CityState = new CityState("Odessa", "WA");;
			s.Save(xam);

			Enrolment enrolment = new Enrolment();
			enrolment.Course = course;
			enrolment.CourseCode = course.CourseCode;
			enrolment.Semester = 1;
			enrolment.Year = 1999;
			enrolment.Student = xam;
			enrolment.StudentNumber = xam.StudentNumber;
			xam.Enrolments.Add(enrolment);
			s.Save(enrolment);

			enrolment = new Enrolment();
			enrolment.Course = course;
			enrolment.CourseCode = course.CourseCode;
			enrolment.Semester = 3;
			enrolment.Year = 1998;
			enrolment.Student = gavin;
			enrolment.StudentNumber = gavin.StudentNumber;
			gavin.Enrolments.Add(enrolment);
			s.Save(enrolment);
			s.Flush();
	
			object result = s.CreateCriteria<Student>()
				.SetProjection(Projections.GroupProperty("CityState"))
				.UniqueResult();
			
			Assert.That(result, Is.InstanceOf<CityState>());
			Assert.That(((CityState)result).City, Is.EqualTo("Odessa"));
			Assert.That(((CityState)result).State, Is.EqualTo("WA"));
	
			result = s.CreateCriteria<Student>("st")
				.SetProjection(Projections.GroupProperty("st.CityState"))
				.UniqueResult();

			Assert.That(result, Is.InstanceOf<CityState>());
			Assert.That(((CityState)result).City, Is.EqualTo("Odessa"));
			Assert.That(((CityState)result).State, Is.EqualTo("WA"));
	
			result = s.CreateCriteria<Student>("st")
				.SetProjection(Projections.GroupProperty("st.CityState"))
				.AddOrder(Order.Asc("CityState"))
				.UniqueResult();
			
			Assert.That(result, Is.InstanceOf<CityState>());
			Assert.That(((CityState)result).City, Is.EqualTo("Odessa"));
			Assert.That(((CityState)result).State, Is.EqualTo("WA"));
	
			result = s.CreateCriteria<Student>("st")
				.SetProjection(Projections.GroupProperty("st.CityState").As("cityState"))
				.AddOrder(Order.Asc("cityState"))
				.UniqueResult();
			
			Assert.That(result, Is.InstanceOf<CityState>());
			Assert.That(((CityState)result).City, Is.EqualTo("Odessa"));
			Assert.That(((CityState)result).State, Is.EqualTo("WA"));
	
			result = s.CreateCriteria<Student>("st")
				.SetProjection(Projections.GroupProperty("st.CityState").As("cityState"))
				.AddOrder(Order.Asc("cityState"))
				.UniqueResult();
			
			Assert.That(result, Is.InstanceOf<CityState>());
			Assert.That(((CityState)result).City, Is.EqualTo("Odessa"));
			Assert.That(((CityState)result).State, Is.EqualTo("WA"));
	
			result = s.CreateCriteria<Student>("st")
				.SetProjection(Projections.GroupProperty("st.CityState").As("cityState"))
				.Add(Restrictions.Eq("st.CityState", new CityState("Odessa", "WA")))
				.AddOrder(Order.Asc("cityState"))
				.UniqueResult();
			
			Assert.That(result, Is.InstanceOf<CityState>());
			Assert.That(((CityState)result).City, Is.EqualTo("Odessa"));
			Assert.That(((CityState)result).State, Is.EqualTo("WA"));
	
			IList list = s.CreateCriteria<Enrolment>()
				.CreateAlias("Student", "st")
				.CreateAlias("Course", "co")
//.........这里部分代码省略.........
开发者ID:NikGovorov,项目名称:nhibernate-core,代码行数:101,代码来源:CriteriaQueryTest.cs

示例13: DistinctProjectionsOfComponents

		public void DistinctProjectionsOfComponents()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();

			Course course = new Course();
			course.CourseCode = "HIB";
			course.Description = "Hibernate Training";
			s.Save(course);

			Student gavin = new Student();
			gavin.Name = "Gavin King";
			gavin.StudentNumber = 667;
			gavin.CityState = new CityState("Odessa", "WA");;
			s.Save(gavin);

			Student xam = new Student();
			xam.Name = "Max Rydahl Andersen";
			xam.StudentNumber = 101;
			xam.PreferredCourse = course;
			xam.CityState = new CityState("Odessa", "WA");;
			s.Save(xam);

			Enrolment enrolment = new Enrolment();
			enrolment.Course = course;
			enrolment.CourseCode = course.CourseCode;
			enrolment.Semester = 1;
			enrolment.Year = 1999;
			enrolment.Student = xam;
			enrolment.StudentNumber = xam.StudentNumber;
			xam.Enrolments.Add(enrolment);
			s.Save(enrolment);

			enrolment = new Enrolment();
			enrolment.Course = course;
			enrolment.CourseCode = course.CourseCode;
			enrolment.Semester = 3;
			enrolment.Year = 1998;
			enrolment.Student = gavin;
			enrolment.StudentNumber = gavin.StudentNumber;
			gavin.Enrolments.Add(enrolment);
			s.Save(enrolment);
			s.Flush();
			
			object result = s.CreateCriteria<Student>()
				.SetProjection(Projections.Distinct(Property.ForName("CityState")))
				.UniqueResult();
			
			Assert.That(result, Is.InstanceOf<CityState>());
			Assert.That(((CityState)result).City, Is.EqualTo("Odessa"));
			Assert.That(((CityState)result).State, Is.EqualTo("WA"));
	
			result = s.CreateCriteria<Student>()
				.SetProjection(Projections.Distinct(Property.ForName("CityState").As("cityState")))
				.AddOrder(Order.Asc("cityState"))
				.UniqueResult();
			
			Assert.That(result, Is.InstanceOf<CityState>());
			Assert.That(((CityState)result).City, Is.EqualTo("Odessa"));
			Assert.That(((CityState)result).State, Is.EqualTo("WA"));
	
			result = s.CreateCriteria<Student>()
				.SetProjection(Projections.Count("CityState.City"))
				.UniqueResult();
			
			Assert.That(result, Is.EqualTo(2));
	
			result = s.CreateCriteria<Student>()
				.SetProjection(Projections.CountDistinct("CityState.City"))
				.UniqueResult();
			
			Assert.That(result, Is.EqualTo(1));
			
			t.Commit();
			s.Close();

//			s = OpenSession();
//			t = s.BeginTransaction();
//			try
//			{
//				result = s.CreateCriteria<Student>()
//					.SetProjection(Projections.Count("CityState"))
//					.UniqueResult();
//				
//				if (!Dialect.SupportsTupleCounts)
//				{
//					fail( "expected SQLGrammarException" );
//				}
//				
//				Assert.That((long)result, Is.EqualTo(1L));
//			}
//			catch (NHibernate.Exceptions.SQLGrammarException ex)
//			{
//				throw ex;
//				if (!Dialect.SupportsTupleCounts)
//				{
//					// expected
//				}
//				else 
//				{
//.........这里部分代码省略.........
开发者ID:NikGovorov,项目名称:nhibernate-core,代码行数:101,代码来源:CriteriaQueryTest.cs

示例14: Subselect

		public void Subselect()
		{
			ISession session = OpenSession();
			ITransaction t = session.BeginTransaction();

			Course course = new Course();
			course.CourseCode = "HIB";
			course.Description = "Hibernate Training";
			session.Save(course);

			Student gavin = new Student();
			gavin.Name = "Gavin King";
			gavin.StudentNumber = 232;
			session.Save(gavin);

			Enrolment enrolment2 = new Enrolment();
			enrolment2.Course = course;
			enrolment2.CourseCode = course.CourseCode;
			enrolment2.Semester = 3;
			enrolment2.Year = 1998;
			enrolment2.Student = gavin;
			enrolment2.StudentNumber = gavin.StudentNumber;
			gavin.Enrolments.Add(enrolment2);
			session.Save(enrolment2);

			DetachedCriteria dc = DetachedCriteria.For(typeof(Student))
				.Add(Property.ForName("StudentNumber").Eq(232L))
				.SetProjection(Property.ForName("Name"));

			if (TestDialect.SupportsOperatorAll)
			{
				session.CreateCriteria(typeof (Student))
					.Add(Subqueries.PropertyEqAll("Name", dc))
					.List();
			}

			session.CreateCriteria(typeof(Student))
				.Add(Subqueries.Exists(dc))
				.List();

			if (TestDialect.SupportsOperatorAll)
			{
				session.CreateCriteria(typeof (Student))
					.Add(Property.ForName("Name").EqAll(dc))
					.List();
			}

			session.CreateCriteria(typeof(Student))
				.Add(Subqueries.In("Gavin King", dc))
				.List();

			DetachedCriteria dc2 = DetachedCriteria.For(typeof(Student), "st")
				.Add(Property.ForName("st.StudentNumber").EqProperty("e.StudentNumber"))
				.SetProjection(Property.ForName("Name"));

			session.CreateCriteria(typeof(Enrolment), "e")
				.Add(Subqueries.Eq("Gavin King", dc2))
				.List();

			DetachedCriteria dc3 = DetachedCriteria.For(typeof(Student), "st")
				.CreateCriteria("Enrolments")
				.CreateCriteria("Course")
				.Add(Property.ForName("Description").Eq("Hibernate Training"))
				.SetProjection(Property.ForName("st.Name"));

			session.CreateCriteria(typeof(Enrolment), "e")
				.Add(Subqueries.Eq("Gavin King", dc3))
				.List();

			DetachedCriteria courseCriteria = DetachedCriteria.For(typeof(Course))
				.Add(Property.ForName("Description").Eq("Hibernate Training"))
				.SetProjection(Projections.Property("CourseCode"));

			DetachedCriteria enrolmentCriteria = DetachedCriteria.For(typeof(Enrolment))
				.Add(Property.ForName("CourseCode").Eq(courseCriteria))
				.SetProjection(Projections.Property("CourseCode"));

			DetachedCriteria studentCriteria = DetachedCriteria.For(typeof(Student))
				.Add(Subqueries.Exists(enrolmentCriteria));

			object result = studentCriteria.GetExecutableCriteria(session).UniqueResult();
			Assert.AreSame(gavin, result);

			session.Delete(enrolment2);
			session.Delete(gavin);
			session.Delete(course);
			t.Commit();
			session.Close();
		}
开发者ID:NikGovorov,项目名称:nhibernate-core,代码行数:89,代码来源:CriteriaQueryTest.cs

示例15: AliasJoinCriterion

		public void AliasJoinCriterion()
		{
			using (ISession session = this.OpenSession())
			{
				using (ITransaction t = session.BeginTransaction())
				{
					Course courseA = new Course();
					courseA.CourseCode = "HIB-A";
					courseA.Description = "Hibernate Training A";
					session.Persist(courseA);
					
					Course courseB = new Course();
					courseB.CourseCode = "HIB-B";
					courseB.Description = "Hibernate Training B";
					session.Persist(courseB);

					Student gavin = new Student();
					gavin.Name = "Gavin King";
					gavin.StudentNumber = 232;
					gavin.PreferredCourse = courseA;
					session.Persist(gavin);

					Student leonardo = new Student();
					leonardo.Name = "Leonardo Quijano";
					leonardo.StudentNumber = 233;
					leonardo.PreferredCourse = courseB;
					session.Persist(leonardo);

					Student johnDoe = new Student();
					johnDoe.Name = "John Doe";
					johnDoe.StudentNumber = 235;
					johnDoe.PreferredCourse = null;
					session.Persist(johnDoe);

					// test == on one value exists
					IList<string> result = session.CreateCriteria<Student>()
						.CreateAlias("PreferredCourse", "pc", JoinType.LeftOuterJoin,
							Restrictions.Eq("pc.CourseCode", "HIB-A"))
						.SetProjection(Property.ForName("pc.CourseCode"))
						.AddOrder(Order.Asc("pc.CourseCode"))
						.List<string>();
					
					// can't be sure of NULL comparison ordering aside from they should
					// either come first or last
					if (result[0] == null)
					{
						Assert.IsNull(result[1]);
						Assert.AreEqual("HIB-A", result[2]);
					}
					else
					{
						Assert.IsNull(result[2]);
						Assert.IsNull(result[1]);
						Assert.AreEqual("HIB-A", result[0]);
					}

					// test == on non existent value
					result = session.CreateCriteria<Student>()
						.CreateAlias("PreferredCourse", "pc", JoinType.LeftOuterJoin,
							Restrictions.Eq("pc.CourseCode", "HIB-R"))
						.SetProjection(Property.ForName("pc.CourseCode"))
						.AddOrder(Order.Asc("pc.CourseCode"))
						.List<string>();

					Assert.AreEqual(3, result.Count);
					Assert.IsNull(result[2]);
					Assert.IsNull(result[1]);
					Assert.IsNull(result[0]);

					// test != on one existing value
					result = session.CreateCriteria<Student>()
						.CreateAlias("PreferredCourse", "pc", JoinType.LeftOuterJoin,
							Restrictions.Not(Restrictions.Eq("pc.CourseCode", "HIB-A")))
						.SetProjection(Property.ForName("pc.CourseCode"))
						.AddOrder(Order.Asc("pc.CourseCode"))
						.List<string>();

					Assert.AreEqual(3, result.Count);

					// can't be sure of NULL comparison ordering aside from they should
					// either come first or last
					if (result[0] == null)
					{
						Assert.IsNull(result[1]);
						Assert.AreEqual("HIB-B", result[2]);
					}
					else
					{
						Assert.AreEqual("HIB-B", result[0]);
						Assert.IsNull(result[1]);
						Assert.IsNull(result[2]);
					}

					// test != on one existing value (using clone)
					var criteria = session.CreateCriteria<Student>()
						.CreateAlias("PreferredCourse", "pc", JoinType.LeftOuterJoin,
						             Restrictions.Not(Restrictions.Eq("pc.CourseCode", "HIB-A")))
						.SetProjection(Property.ForName("pc.CourseCode"))
						.AddOrder(Order.Asc("pc.CourseCode"));
					var clonedCriteria = CriteriaTransformer.Clone(criteria);
//.........这里部分代码省略.........
开发者ID:NikGovorov,项目名称:nhibernate-core,代码行数:101,代码来源:CriteriaQueryTest.cs


注:本文中的NHibernate.Test.Criteria.Student类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。