internal List<FilteredHistoryResponsetoStudent> GetHistoryForStudent(Entity objReq) { using (var db = new WizzDataContext()) { List<FilteredHistoryResponsetoStudent> studentList = new List<FilteredHistoryResponsetoStudent>(); List<FilteredHistoryResponsetoStudent> responseList = new List<FilteredHistoryResponsetoStudent>(); int userId = Convert.ToInt32(objReq.userId); var sessionList = db.usp_GetHistoryForStudent(userId).ToList(); List<usp_GetHistoryForStudentResult> ListSession = new List<usp_GetHistoryForStudentResult>(); // var subList = sessionList.Select(s => s.subjectId).Distinct().ToList(); if (sessionList == null || sessionList.Count == 0) { return studentList; } foreach (var n in sessionList) { var data = sessionList.Where(x => x.uniqueRequestId == n.uniqueRequestId).ToList(); if (data != null && data.Count > 1) { if (!ListSession.Any(x => x.uniqueRequestId == n.uniqueRequestId)) { var count = sessionList.Where(x => x.uniqueRequestId == n.uniqueRequestId).Sum(x => x.TotalMinutes); var avgRating = sessionList.Where(x => x.uniqueRequestId == n.uniqueRequestId).Average(x => x.tutorRating); data[0].TotalMinutes = count; data[0].tutorRating = avgRating; ListSession.Add(data[0]); } // var tutorRating = sessionList.Where(x => x.uniqueRequestId == n.uniqueRequestId); //int totalMins = 0; //decimal rat = 0; //foreach (var val in count) { // totalMins += Convert.ToInt32(val.TotalMinutes); // rat += Convert.ToDecimal(val.tutorRating); //} //sessionList.RemoveAll(x => x.uniqueRequestId == n.uniqueRequestId); } else { if (!ListSession.Any(x=>x.uniqueRequestId==n.uniqueRequestId)) ListSession.Add(n); } } //List<String> uniqueList = new List<string>(); //foreach (var n in sessionList) //{ // uniqueList.Add(n.uniqueRequestId); //} //int i = 0; foreach (var c in ListSession) { var list = ListSession.Where(s => s.subjectId == c.subjectId).ToList(); if (!studentList.Any(x => x.subjectId == c.subjectId.ToString())) { FilteredHistoryResponsetoStudent FilteredObj = new FilteredHistoryResponsetoStudent(); FilteredObj.subjectId = c.subjectId.ToString(); FilteredObj.subjectType = c.subjectType.ToString(); FilteredObj.subjectName = c.subjectName.ToString(); List<HistoryResponsetoStudent> model = new List<HistoryResponsetoStudent>(); foreach (var sess in ListSession) { //var data = studentList.DetailsList.Where(x => x.uniqueRequestId == sess.uniqueRequestId).FirstOrDefault(); //if (data == null) //{ model = (from r in ListSession.Where(x => x.subjectId == c.subjectId) select new HistoryResponsetoStudent { // studentList.Where(x=>x.) tutorId = r.tutorId.ToString(), tutorName = r.tutorName, sessionId = r.sessionId.ToString(), tutorRating = Convert.ToString(r.tutorRating), totalHours = r.TotalMinutes.ToString(), profilepic = r.profilePic.ToString(), searchCode = r.searchCode.ToString(), uniqueRequestId = r.uniqueRequestId.ToString(), }).ToList(); // } //else { // data.totalHours = Convert.ToString(Convert.ToInt32(data.totalHours) + sess.TotalMinutes); // data.tutorRating = Convert.ToString((Convert.ToInt32(data.tutorRating) + sess.tutorRating)/2); //} } FilteredObj.DetailsList = model; studentList.Add(FilteredObj); } } //foreach (var n in studentList) //{ // foreach (var p in n.DetailsList) { // var data = n.DetailsList.Where(x => x.uniqueRequestId == p.uniqueRequestId).FirstOrDefault(); // if(data==null){ // responseList.Add(n) ; // }else{ // data.totalHours = Convert.ToString(Convert.ToInt32(data.totalHours) +Convert.ToInt32(p.totalHours)); // // data.tutorRating = Convert.ToString((Convert.ToInt32(data.tutorRating) + Convert.ToInt32(p.tutorRating)) / 2); // n.DetailsList.Add(data); // responseList.Add(n); // } // } //} return studentList; } }