Dawood Abbas

Dawood Abbas

  • NA
  • 264
  • 98.4k

How to Select the Highest value in each Group in linq?

Dec 29 2015 2:24 AM
I tried like below but showing an error, so how to set values to class properties in select?
 
public class CUSTOM_STU_DETAIL
{
public string Re_Admission_Type { get; set; }
public DateTime? Re_Admission_Date { get; set; }
public long? Registration_Id { get; set; }
public string Admission_No { get; set; }
public long? Class_Id { get; set; }
public long? Section_Id { get; set; }
public long? Academic_Year_Id { get; set; }
public string Student_First_Name { get; set; }
public string Student_Last_Name { get; set; }
public DateTime? Date_Of_Birth { get; set; }
public string Gender { get; set; }
public string Blood_Group { get; set; }
public long? Cast_Id { get; set; }
public long? Religion_Id { get; set; }
public long? Roll_Id { get; set; }
public string Set_New_RollNo { get; set; }
public long? Company_ID { get; set; }
public long? COMPANY_LOCATION_ID { get; set; }
public string STUDENT_NAME { get; set; }
//public DateTime Attendance_Date { get; set; }
public string Academic_Year_Name { get; set; }
public string Class_Name { get; set; }
public string Section_Name { get; set; }
}
List<CUSTOM_STU_DETAIL> academicYr = new List<CUSTOM_STU_DETAIL>();
if (CompanyId != null && CompanyId != 0 && CompanyLocationId != null && CompanyLocationId != 0 && ClassID != null && ClassID != 0 && SectionID != null && SectionID != 0)
{
academicYr = (from s in db.Student_Re_Admission
group s by s.Registration_Id into stugrp
let topp = stugrp.Max(x => x.Re_Admission_Id)
select new CUSTOM_STU_DETAIL
{
Re_Admission_Type = topp.Re_Admission_Type,
Re_Admission_Date = b.Re_Admission_Date,
Registration_Id = b.Registration_Id,
Admission_No = b.Admission_No,
Class_Id = ClassID,
Section_Id = SectionID,
Academic_Year_Id = b.Academic_Year_Id,
Student_First_Name = b.Student_First_Name,
Student_Last_Name = b.Student_Last_Name,
Date_Of_Birth = b.Date_Of_Birth,
Gender = b.Gender,
Blood_Group = b.Blood_Group,
Cast_Id = b.Cast_Id,
Religion_Id = b.Religion_Id,
Roll_Id = a.Roll_Id,
Set_New_RollNo = a.Set_New_RollNo,
Company_ID = CompanyId,
COMPANY_LOCATION_ID = CompanyLocationId,
STUDENT_NAME = a.Student_First_Name + " " + " " + a.Student_Last_Name,
}).ToList();
 

Answers (2)