• nhibernate to linq


    nhibernate在使用过程中遇到问题,不断解决后的一些零碎知识:

    一、Express:

      Express声明一个变量后,此变量可以保存一个lamda表达式,Express exp=(o=>o.fieldname),这样可以动态组合要查询的字段。

    上代码:

    Expression<Func<Gx.Model.Author, bool>> where1, where2;
    if (CityId != 0)
    {
    // strCity = " and other_City=" + CityId;
    where1 = o => o.OtherCity == CityId;
    }
    else where1 = o => o.AuthorId != null;
    if (Types)
    {
    //strPhoto = " and Photo1 is not null ";
    where2 = o => o.Photo1 != null;

    }
    else where2 = o => o.AuthorId != null;
    var list1 = new Author().CreateQueryOver().Where(where1)

    .Where(where2)
    .OrderBy(o=>o.AuthorId).Desc

    .Take(NO)
    .List<Gx.Model.Author>();//获取最新加入的成员

     

    二、连表后,排序的问题。

    IList<Gx.Model.MessageBoard> MessBoard = (from m in iq2
    //from t in m.MessageBoards
    // .OrderByDescending(o=>o.MessageBoards.Select(oo=>oo.MBMessageDate))
    //from t in m.Orders
    join m2 in iq
    on m.AuthorId equals m2.Author.AuthorId into m3

    from t in m3
    orderby t.AuthorID
    select new Gx.Model.MessageBoard { Mb_id = t.Mb_id, FromID = t.FromID, Realname = t.FromID != 0 ? t.Author.RealName : t.Realname, MBIP = t.FromID != 0 ? t.Author.OtherAddress : t.MBIP ,MBMessage=t.MBMessage,MBMessageDate=t.MBMessageDate, Author=t.Author})
    //.Orderby(o=>o.AuthorID)
    .Take(Count)
    .ToList<Gx.Model.MessageBoard>();

    像上面两处红色字体那样排序都会报错(Invalid Path)。

    解决办法:

    MessBoard=MessBoard..Orderby(o=>o.AuthorID).ToList();

    三、nhibernate完成与case when相同的功能:

    条件:必须在数据访问返回的是一个IQueryable,linq查询(方便)。

     select new Gx.Model.MessageBoard { Mb_id = t.Mb_id, FromID = t.FromID, Realname = t.FromID != 0 ? t.Author.RealName : t.Realname, MBIP = t.FromID != 0 ? t.Author.OtherAddress : t.MBIP ,MBMessage=t.MBMessage,MBMessageDate=t.MBMessageDate, Author=t.Author})

    以上,只是些零散的随笔。有不正确之处,批评指正。

  • 相关阅读:
    java 与打卡器通过udp协议交互
    java串口通信与打卡器交互
    hibernate 学习小结
    Log4J使用说明
    【秋招必备】Git常用命令(2021最新版)
    【秋招必备】Java集合面试题(2021最新版)
    工作这么多年!很多人竟然不知道线程池的创建方式有7种?
    【秋招必备】Java虚拟机面试题(2021最新版)
    【秋招必备】java异常面试题(2021最新版)
    好未来面试官:说说强引用、软引用、弱引用、幻象引用有什么区别?
  • 原文地址:https://www.cnblogs.com/zhuhoumo/p/nhibernate.html
Copyright © 2020-2023  润新知