zoukankan      html  css  js  c++  java
  • nhibernate3 linq的的select 操作 YI

    ling 的select 操作1 ,看了2天nhibernate的官方网站,基本方法测试了。等有空把nhibernate的源码下拉研究下

    View Code
    using System;
    using System.Linq;
    using NHibernate.Transform;
    using NUnit.Framework;
    using NetworkMonitor.DAL.Business.Model;
    
    namespace NetworkMonitor.NunitTest.DAlTets
    {
        public class MyUser
        {
            public string 姓名 { set; get; }
    
            public string 密码 { set; get; }
    
            public int Tj { set; get; }
    
            public DateTime MaxDate { set; get; }
    
    
        }
    
        [TestFixture]
        public class 数据库操作LingSelect
        {
            private readonly NetworkMonitor.DAL.Core.MyServiceFactory _service = NetworkMonitor.DAL.Core.MyServiceFactory.Instance;
    
            #region 获取一个字段
            [Test]
            public void Select操作()
            {
                var bb = _service.GetTbUserService.Dao();
    
    
                var list1 =
                bb.SessionFactory.GetSession().QueryOver<TbUser>().
                    Select(c => c.UserName).List<string>();
    
                foreach (var str in list1)
                {
                    System.Console.WriteLine(str.ToString());
                }
    
            }
            #endregion
    
    
            #region 获取几个字段
            [Test]
            public void select获取对象到零时list()
            {
                var bb = _service.GetTbUserService.Dao();
           
                var list2 =
                bb.SessionFactory.GetSession().QueryOver<TbUser>().
                    //1.设置查询
                Select(
                         c => c.UserName,
                      c => c.LoginPWD)
                    .List<object[]>()
                    //2.设置查询结果
                    .Select(
                    pro => new
                    {
                        姓名 = (string)pro[0],
                        密码 = (string)pro[1]
                    }
                    );
    
                foreach (var str in list2)
                {
                    System.Console.WriteLine(str.姓名);
                    System.Console.WriteLine(str.密码);
                }
    
            }
            #endregion
    
            #region 获取几个字段--到对象
            [Test]
            public void select获取到对象()
            {
                var bb = _service.GetTbUserService.Dao();
             
                var list3 =
                bb.SessionFactory.GetSession().QueryOver<TbUser>().
                    //1.设置查询
                Select(
                         c => c.UserName,
                      c => c.LoginPWD)
                    .List<object[]>()
                    //2.设置查询结果到对象
                    .Select(
                    pro => new MyUser()
                    {
                        姓名 = (string)pro[0],
                        密码 = (string)pro[1]
                    }
                    );
                    
    
                foreach (var str in list3)
                {
                    System.Console.WriteLine(str.姓名);
                    System.Console.WriteLine(str.密码);
                }
    
    
            }
            #endregion
    
            #region 组合测组合select到objectList
    
            [Test]
            public void 测组合select到objectList()
            {
                  var bb = _service.GetTbUserService.Dao();
    
    
                var list =
                    bb.SessionFactory.GetSession().QueryOver<TbUser>()
                        .SelectList(
                            //指定选择字段
                        ar => ar.Select(c => c.UserName) //ar.Select(c => c.GroupName)报错和group有冲突
                                  //指定条件字段
                                  .SelectCount(c => c.Id)//统计 
    
                                  .SelectGroup(c => c.UserName )//group 这里一定要主意前面select可以group
    
                                  .SelectMax(c => c.Createdate)//
    
    
                        ).List<object[]>();
                foreach (var ob in list)
                {
                    //顺序就是你自己select的顺序
                    string username = (string) ob[0];
    
                    int tj = (int) ob[1];
    
                    string username2 = (string)ob[2];
    
                    DateTime max = (DateTime) ob[3];
    
                    System.Console.WriteLine("uname ={0}, tj={1} gpuname={2} maxD = {3}",username,tj,username2,max);
    
                }
            }
    
            #endregion
    
            #region 测组合select到指定对象List
    
            [Test]
            public void 测组合select到指定对象List()
            {
                var bb = _service.GetTbUserService.Dao();
    
    
                MyUser myUser = null;
                var list =
                    bb.SessionFactory.GetSession().QueryOver<TbUser>()
                        .SelectList(
                    //指定选择字段
                        ar => ar
                                  //.Select(c => c.UserName).WithAlias(()=>myUser.姓名)
                            //指定条件字段
                                  .SelectCount(c => c.Id).WithAlias(()=>myUser.Tj)//统计 结合group 使用
    
                                  .SelectGroup(c => c.UserName).WithAlias(()=>myUser.姓名)//group
    
                                  .SelectMax(c => c.Createdate).WithAlias(()=>myUser.MaxDate)//
    
    
                        )
                        .TransformUsing(Transformers.AliasToBean<MyUser>())
                        .List<MyUser>();
    
                foreach (var user in list)
                {
                    Console.WriteLine("uname ={0}, tj={1} gpuname={2} maxD = {3}",user.姓名,user.Tj,user.姓名,user.MaxDate);
                }
            }
    
            #endregion
        }
    }
  • 相关阅读:
    编程基础之Java中继承和多态的理解
    2020年程序员是否过剩?
    如何自学编程,零基础适合学习Java或者Web前端吗,非科班的能学java吗?
    转行IT做后端开发,学python还是java?
    java和大数据哪个更有前途啊?零基础适合学哪个课程?
    随笔(二十)
    随笔(十九)
    随笔(十八)
    OSPF练习
    OSPF综合实验(二)
  • 原文地址:https://www.cnblogs.com/loswing/p/2475514.html
Copyright © 2011-2022 走看看