I am having a view model
public class HouseReportMainModel
{
public int HouseId { get; set; }
public int DisplayOrder { get; set; }
public string HouseName { get; set; }
public string HouseColor{ get; set; }
public int TotalStudentCount { get; set; }
public List<HouseReportViewModel> StudentList { get; set; }
}
I will get the student list using linq and i need to fetch the count of the inner list and need to display that in parent view model
var houseReportDetails = _dbContext.Set<HouseStudentMapping>()
.AsNoTracking()
.Include(i=>i.StudentClassMap.Student)
.Include(i=>i.StudentClassMap.Class.Standard)
.Include(i=>i.House)
.Where(s => selectedClassIds.Contains(s.StudentClassMap.ClassId ?? 0) &&
selectedHouseIds.Contains(s.HouseId))
.GroupBy(s => s.HouseId)
.Select(s => new HouseReportMainModel()
{
HouseId = s.Key,
DisplayOrder = s.Select(c=>c.House.DisplayOrder).FirstOrDefault(),
HouseName = s.Select(c => c.House.Name).FirstOrDefault(),
HouseColor = s.Select(c => c.House.Color).FirstOrDefault(),
StudentList = s.Select(t => new HouseReportViewModel()
{
StudentClassMapId = t.StudentClassMapId,
RollNumber = t.StudentClassMap.RollNumber??0,
StudentName = t.StudentClassMap.Student.Name,
ClassName = t.StudentClassMap.Class.Standard.Name+" "+ t.StudentClassMap.Class.Name,
HouseId = t.HouseId,
HouseName = t.House.Name,
ClassId = t.StudentClassMap.ClassId??0,
StandardId = t.StudentClassMap.Class.StandardId
}).ToList()
}).ToList();
I need TotalStudentCount = StudentList.Sum(x => x.Distinct().Count()
But how will i get the TotalStudentCount inside the linq list
Could someone please help me.