*******************BEGIN*******************

YI-D

恶魔的眉眼

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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
    }
}
posted on 2012-04-28 17:40  YI-D  阅读(303)  评论(0编辑  收藏  举报

^_^ ^_^ *******************END******************* ^_^ ^_^