public UinversityDTOList GetUniversitiesByName(string searchName) { var unis = context.Universities; var uinversities = new UinversityDTOList() { Universities = unis.ToList().Select(c => new UniversityDTO { Id = c.UniversityId, Name = c.Name, Address = c.Address, StudentCount = unis.Include(st => st.Students). Where(un => un.UniversityId == c.UniversityId).FirstOrDefault(). Students.Count() }).Where(uni => uni.Name.Contains(searchName)).ToList(), UniversitiesCount = unis.ToList().Count }; return(uinversities); }
public UinversityDTOList GetUniversities() { #region //UinversityDTOList uinversityDTOList = new UinversityDTOList(); //uinversityDTOList.Universities = new List<UniversityDTO>(); //var universities = context.Universities.ToList(); //foreach (var uni in universities) //{ // var uniDto = new UniversityDTO(); // uniDto.Id = uni.Id; // uniDto.Address = uni.Address; // uniDto.Name = uni.Name; // uniDto.StudentCount = context.Universities.Include(st => st.Students) // .Where(un => un.Id == uni.Id).FirstOrDefault(). // Students. // Count(); // // uniDto.StudentCount = context.Students.Count(st => st.UniversityId == uni.Id); // uinversityDTOList.Universities.Add(uniDto); //} //uinversityDTOList.UniversitiesCount = universities.Count; //return uinversityDTOList; #endregion var unis = context.Universities; var uinversities = new UinversityDTOList() { Universities = unis.ToList().Select(c => new UniversityDTO { Id = c.UniversityId, Name = c.Name, Address = c.Address, StudentCount = unis.Include(st => st.Students). Where(un => un.UniversityId == c.UniversityId).FirstOrDefault(). Students. Count() }).ToList(), UniversitiesCount = unis.ToList().Count }; return(uinversities); }