zoukankan      html  css  js  c++  java
  • 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})

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

  • 相关阅读:
    C++随机迷宫生成[转载]
    浮点指令
    表盘
    TabControl+ListView
    ListView
    Tooltips2
    随机数
    Tooltips
    iOS9 http不能访问网络——在Xcode中将https改成http方式
    iOS开发——音频篇——音效的播放
  • 原文地址:https://www.cnblogs.com/zhuhoumo/p/nhibernate.html
Copyright © 2011-2022 走看看