using NHibernate.Criterion; using NUnit.Framework; using NetworkMonitor.DAL.Business.Model; namespace NetworkMonitor.NunitTest.DAlTets { class 数据库操作LingWhere { //private NetworkMonitor.DAL.Core.MyServiceFactory. private readonly NetworkMonitor.DAL.Core.MyServiceFactory _service = NetworkMonitor.DAL.Core.MyServiceFactory.Instance; [Test] public void Where基本操作() { var bb = _service.GetTbUserService.Dao(); bb.SessionFactory.GetSession().QueryOver<TbUser>(). //每个where之间用 and自动连接 // WHERE (this_.UserName = @p0 and this_.UserName = @p1) Where(c => c.UserName == "test" && c.UserName == "10") .And(c => c.UserName == "99"|| c.UserName == "") /* * 建议 一次都放到where里面 */ //排序 .OrderBy(c => c.UserName).Asc .List(); } [Test] public void Where级联操作() { var bb = _service.GetTbUserService.Dao(); //准备级联条件-- /**/ QueryOver<TbUser> lr刘瑞 = QueryOver.Of<TbUser>() .Where(oo => oo.UserName =="刘瑞").Select(oo =>oo.UserName); TbUser tbUser = null; var list = bb.SessionFactory.GetSession().QueryOver<TbUser>() .WithSubquery.WhereProperty(c => c.UserName).NotIn(lr刘瑞)//没有刘瑞的数据 //排序 //.OrderBy(c => c.UserName).Asc .List(); foreach (var user in list) { // Assert.AreEqual(user.UserName,"刘瑞"); System.Console.WriteLine(user.UserName); } } [Test] public void Where多表联操作() { var bb = _service.GetTbUserService.Dao(); //准备级联条件-- /**/ QueryOver<TbUserRole> lrXX = QueryOver.Of<TbUserRole>(). Select(oo => oo.UId); TbUser tbUser = null; var list = bb.SessionFactory.GetSession().QueryOver<TbUser>() .WithSubquery.WhereProperty(c => c.Id).Eq(lrXX)//只有XX的数据 //排序 //.OrderBy(c => c.UserName).Asc .List(); foreach (var user in list) { // Assert.AreEqual(user.UserName,"X11X"); System.Console.WriteLine(user.UserName); } } } }