//根据学生ID返回Contacts List public JsonResult GetContacts(string studentID) { List<EasyChatTimeModel> contacts = new List<EasyChatTimeModel>(); //以数组形式存储所有联系人的联系时间 IEnumerable<EasyChatTimeEntity>[] chatTimes = null; //每个联系人的联系信息 ContactIdentity contactInfo = null; //所有联系人列表,从数据库中读取赋值 IEnumerable<StudentParentEntity> parents = null; if (studentID != null && studentID != string.Empty && studentID != Guid.Empty.ToString()) { Guid id = new Guid(studentID); parents = repository.StudentParent.Where(s => s.StudentID == id); } if (parents.Count() > 0) { //初始化所有联系人联系时间的数组长度 chatTimes = new IEnumerable<EasyChatTimeEntity>[parents.Count()]; for (int i = 0; i < parents.Count(); i++) { StudentParentEntity parent = parents.ElementAt(i); contactInfo = new ContactIdentity { PersonIdentity = parent.PersonIdentity.ToString(), NameCn = parent.NameCn, Mobile = parent.Mobile, Email = parent.Email, QQ = parent.QQ, Weixin = parent.Weixin }; chatTimes[i] = repository.EasyChatTime.Where(e => e.IfParentID == parent.ParentID).Select(e => e); contacts.Add(new EasyChatTimeModel { ContactIdentity = contactInfo, EasyChatTimes = chatTimes[i] }); } //以下的写法会导致chatTimes始终变成最后赋值的那个 //foreach (StudentParentEntity parent in parents) //{ // contactInfo = new ContactIdentity // { // PersonIdentity = parent.PersonIdentity.ToString(), // NameCn = parent.NameCn, // Mobile = parent.Mobile, // Email = parent.Email // }; // chatTimes = repository.EasyChatTime.Where(e => e.IfParentID == parent.ParentID).Select(e => e); // contacts.Add(new EasyChatTimeModel { ContactIdentity = contactInfo, EasyChatTimes = chatTimes }); //} } return Json(contacts,JsonRequestBehavior.AllowGet); }
//根据学生ID返回ContactIdentity List public JsonResult GetContactIdentityList(string studentID) { List<ContactIdentity> contacts = new List<ContactIdentity>(); //每个联系人的联系信息 ContactIdentity contactInfo = null; StudentInfoEntity studentInfo = null; //所有联系人列表,从数据库中读取赋值 IEnumerable<StudentParentEntity> parents = null; if (studentID != null && studentID != string.Empty && studentID != Guid.Empty.ToString()) { Guid id = new Guid(studentID); parents = repository.StudentParent.Where(s => s.StudentID == id); studentInfo = repository.StudentsInfo.SingleOrDefault(s => s.StudentID == id); contactInfo = new ContactIdentity { PersonIdentity = "学生", NameCn = studentInfo.NameCn, Mobile = studentInfo.Mobile, Email = studentInfo.Email }; contacts.Add(contactInfo); contactInfo = null; } if (parents.Count() > 0) { for (int i = 0; i < parents.Count(); i++) { StudentParentEntity parent = parents.ElementAt(i); contactInfo = new ContactIdentity { PersonIdentity = parent.PersonIdentity.ToString(), NameCn = parent.NameCn, Mobile = parent.Mobile, Email = parent.Email }; contacts.Add(contactInfo); contactInfo = null; } } return Json(contacts, JsonRequestBehavior.AllowGet); }