繼承new()這個就能夠進行泛型實例化了。原本還想着直接傳一個進去(ActivityPlayRequest request,T model, ActivityDbContext dbContext)html
可是因爲list引用的是地址指針,因此到最後List中的全部數據都是model最後一次的改變結果,比較坑。函數
因此查了一下泛型實例化的寫法。固然這種寫法要求該對象存在無參的構造函數才行。spa
是從這裏參考的:Author ※森林小居※指針
http://www.cnblogs.com/Slxj/archive/2011/10/13/2210443.htmlcode
public List<T> GetAbstractUserDTO<T>(ActivityPlayRequest request,ActivityDbContext dbContext) where T : AbstractUserDTO,new() { //temp用來存放一個值,由於T泛型不能建立 var merchant = dbContext.Find<Merchant>(request.MerchantID); if (merchant == null) { return null; } var result = new List<T>(); var weixinUserList = dbContext.WeixinUsers.ToList(); var merchantWeixinUserList = dbContext.MerchantWeixinUsers.ToList(); if (!string.IsNullOrEmpty(request.Keyword)) { request.Keyword = request.Keyword.Trim(); } //參加用戶的惟一表 #region 存儲值 var playList = dbContext.ActivityScenePlayRecords.Where(u => u.ActivitySceneID == request.ActivitySceneID).ToList(); foreach (var item in playList) { var temp = new T(); temp.MerchantWeixinUserID = item.MerchantWeixinUserID; temp.WeixinUserID = item.WeixinUserID; #region 真實姓名聯繫方式,不經過weixinuser表了,而是使用ActivityScenePlayRecord表進行獲取 temp.RealName = item.RealName; temp.TelPhone = item.Telphone; #endregion var merchantWeixinUser = merchantWeixinUserList.Where(u => u.ID == item.MerchantWeixinUserID).FirstOrDefault(); temp.NickName = merchantWeixinUser.NickName; temp.OpenId = merchantWeixinUser.OpenId; temp.ImageUrl = merchantWeixinUser.HeadImageUrl; #region 根據keyword判斷是否要添加進入 if (!string.IsNullOrEmpty(request.Keyword)) { if (!string.IsNullOrEmpty(temp.RealName) && temp.RealName.Contains(request.Keyword)) { result.Add(temp); } else if (!string.IsNullOrEmpty(temp.TelPhone) && temp.TelPhone.Contains(request.Keyword)) { result.Add(temp); } else if (!string.IsNullOrEmpty(temp.NickName) && temp.NickName.Contains(request.Keyword)) { result.Add(temp); } } else { result.Add(temp); } #endregion } #endregion return result; }