zoukankan      html  css  js  c++  java
  • linq select 实体额外赋值成实体(未测试效率)

    方法:

            /// <summary>
            /// 实体类复制
            /// </summary>
            /// <param name="objold"></param>
            public static dynamic EntityCopy(object objone, object objtow)
            {
                dynamic objnew = new System.Dynamic.ExpandoObject();
                Type myType = objone.GetType();
                Type myType2 = objtow.GetType();
                PropertyInfo currobj = null;
    
                PropertyInfo[] myProperties = myType.GetProperties();
                for (int i = 0; i < myProperties.Length; i++)
                {
                    currobj = objone.GetType().GetProperties()[i];
                    var ss = currobj.GetValue(objone, null);
                    //currobj.SetValue(objnew, currobj.GetValue(objold, null), null);
                    (objnew as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new KeyValuePair<string, object>(currobj.Name, ss));
                }
    
                myProperties = myType2.GetProperties();
                for (int i = 0; i < myProperties.Length; i++)
                {
                    currobj = objtow.GetType().GetProperties()[i];
                    var ss = currobj.GetValue(objtow, null);
                    //currobj.SetValue(objnew, currobj.GetValue(objold, null), null);
                    (objnew as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new KeyValuePair<string, object>(currobj.Name, ss));
                }
                return objnew;
            }

    使用:

    //数据集关联
                    var data = (from l in lists
                                join u in UnitWork.Find<Base_User>(null) on l.Creater equals u.Id into userrooms
                                from u in userrooms.DefaultIfEmpty()
                                join h in UnitWork.Find<Hotel>(null) on l.H_Id equals h.Id into hotel
                                from h in hotel.DefaultIfEmpty()
                                join t in UnitWork.Find<Base_MessageType>(null) on l.BM_Id equals t.Id into msgtype
                                from t in msgtype.DefaultIfEmpty()
                                select ObjectHelper.EntityCopy(l, new
                                {
                                    BM_Name = t == null ? null : t.Name,
                                    H_Name = h == null ? null : h.Name,
                                    CreaterName = u == null ? null : u.Name
                                })
                                ).ToList();

    结果:

    [
        {
          "Contents": "1",
          "BM_Id": 1,
          "SourceId": 1,
          "ReceiveId": 1,
          "H_Id": 3,
          "Status": 1,
          "Creater": 1,
          "CreateTime": "2020-11-03T00:00:00",
          "Id": 3,
          "BM_Name": null,
          "H_Name": "测试酒店",
          "CreaterName": "管理"
        }
    }
  • 相关阅读:
    作诗(si)[分块]
    【洛谷 P3469】[POI2008]BLO-Blockade(割点)
    【洛谷 P2464】[SDOI2008]郁闷的小J(线段树)
    【BZOJ 3907】网格(Catalan数)
    【洛谷 P4211】[LNOI2014]LCA(树链剖分,差分)
    【洛谷 P2480】 [SDOI2010]古代猪文(中国剩余定理,Lucas定理)
    【洛谷 P3842】[TJOI2007]线段(DP)
    【洛谷 P2346】四子连棋(状态压缩,搜索)
    【洛谷 P1363】幻想迷宫(搜索)
    【洛谷 P1364】医院设置(树的重心)
  • 原文地址:https://www.cnblogs.com/ruiyuan/p/13927205.html
Copyright © 2011-2022 走看看